3

I wonder how C++ is handling variables so that the distance between the two addresses in memory of integer variables declared and initialized one after another is 3537492 - 3537480 = 12 ( I'm assuming bits(?) )

#include <cstdio>

using namespace std;

int main( int argc, char ** argv )
{
    int x = 1;
    int y = 2;

    printf("int:\t%d\n", sizeof(int));
    printf("unsigned int:\t%d\n", sizeof(unsigned int));

    printf("Address of x\n\tHex:\t%p\n\tDec:\t%d\n", &x, &x);
    printf("Address of y\n\tHex:\t%p\n\tDec:\t%d\n", &y, &y);

    return 0;
}

Output:

int:    4
unsigned int:   4
Address of x
        Hex:    0035FA54
        Dec:    3537492
Address of y
        Hex:    0035FA48
        Dec:    3537480
sebastian_t
  • 2,241
  • 6
  • 23
  • 39
  • 4
    It's 12 _bytes_ not bits. – Ryan J Jan 27 '15 at 00:41
  • 2
    The difference you calculated is number of bytes. How C++ handles variables is completely platform, compiler setting specific. In some cases variables aren't in kept in memory at all. – Sedat Kapanoglu Jan 27 '15 at 00:41
  • what is your question? – David Xu Jan 27 '15 at 00:41
  • 2
    Use the option to display the assembly generated by the compiler, and see how it's laying out the memory. – Barmar Jan 27 '15 at 00:43
  • 1
    Your program has undefined behaviour: `%p` expects a `void *` argument, and `%d` expects an `int` argument. – Kerrek SB Jan 27 '15 at 08:56
  • @KerrekSB: Actually I've been asking myself what type of data the address of operator returns by default and correct me if I'm wrong but I think its integer. So in case of printing pointer I should do static_cast? – sebastian_t Jan 28 '15 at 02:16
  • @starach: The address of an lvalue of type `T` is a pointer of type `T *`. – Kerrek SB Jan 28 '15 at 08:23

3 Answers3

1

The distance between two variables in memory is most often a meaningless number. The major exception is two array elements.

This is no exception: the number you get is 12, by coincidence. If you would try to calculate it "blue" would technically be a legal outcome. That's because comparing unrelated pointers is Undefined Behavior, and then literally anything can happen.

Also, the values you get can vary from run to run. (On many modern Operating systems, this is the case for security reasons. It's hard to hack a program when it's moving around all the time)

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

My guess would be that the compiler is aligning the integers along a word boundary for some performance reason. If a word is 128 bits, or 16 bytes, then such behavior would cause a distance of 12 bytes between variables. You would have to understand a bit more about the architecture you are compiling on to know if I'm right. No pun intended. See http://en.wikipedia.org/wiki/Data_structure_alignment

Rich
  • 926
  • 8
  • 17
  • I don't think that can be the case here. Firstly, 12 bytes is the difference between the start addresses of the variables, not the length of the gap between them. Secondly, the hex values show clearly that neither variable is 16 byte aligned. (i.e. the least significant hex digits are not zero.) – Ian Cook Jan 27 '15 at 08:52
1

I suppose you compiled this code in Visual Studio Debug configuration. In this mode compiler does extra memory allocations in between your variables to detect possible stack corruption. Try switching to Release and you will see the distance becomes 4 bytes as it supposed to be. More information on the topic: https://msdn.microsoft.com/en-us/library/8wtf2dfz.aspx

Konstantin
  • 11
  • 2