2

I don't understand why the addresses of my floats go up by 16, when the size of my floats are 4. Could someone please explain?

Code:

char* mychar   = new char[SIZE];
float* myfloat = new float[SIZE];

for(int i = 0; i < SIZE; i++)
{
    mychar[i] = 'A' + i;
    myfloat[i] = 101 + i; 
}

for(int i = 0; i < SIZE; i++)
    {
        cout << setw(12) << "new char @ <"  << static_cast<void*>(mychar) + sizeof(char)*i  << ">=<" << mychar[i]  << ">" 
             << setw(14) << "   new float @ <" << myfloat + sizeof(float)*i                    << ">=<" << myfloat[i] << ">\n"; 
    }

cout<< "Size of float: " << sizeof(float) << endl << "Size of char: " << sizeof(char);

Output:

new char @ <0x800102e8>=<A>   new float @ <0x800102f8>=<101>
new char @ <0x800102e9>=<B>   new float @ <0x80010308>=<102>
new char @ <0x800102ea>=<C>   new float @ <0x80010318>=<103>
new char @ <0x800102eb>=<D>   new float @ <0x80010328>=<104>
Size of float: 4
Size of char: 1
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
jameswoo
  • 386
  • 3
  • 12
  • Why would anyone downvote this........... baffling – Andrey Mishchenko Jan 04 '14 at 00:30
  • Because pointer arithmetic works in terms of units, not bytes. –  Jan 04 '14 at 00:30
  • @Andrey Perhaps because it's an obvious duplicate? (I've seen at least two identical questions asked this week.) –  Jan 04 '14 at 00:31
  • 1
    @DominikC No, just `myfloat + i`. –  Jan 04 '14 at 00:31
  • 1
    @H2CO3 fair, I haven't but I believe you. Although I will say this is not in the category of questions that are trivially easy to search for, especially for a beginner. If you know the phrase "pointer arithmetic" then you know the answer to this already. – Andrey Mishchenko Jan 04 '14 at 00:31
  • Possible duplicate of [C++: Pointer Arithmetic](http://stackoverflow.com/questions/11713929/c-pointer-arithmetic) –  Jan 04 '14 at 00:32
  • @H2CO3 Is my output correct then? – jameswoo Jan 04 '14 at 00:33
  • 2
    @jameswoo Yes. While you're a beginner, the compiler is never wrong. –  Jan 04 '14 at 00:33
  • @jameswoo the reason is that when you add 1 to a pointer, it does not increment the memory address by 1, but by `sizeof(type of object pointed to)`. See the link H2CO3 posted. – Andrey Mishchenko Jan 04 '14 at 00:34
  • See what happens when you simply myfloat + 1 on different types. – ScarletAmaranth Jan 04 '14 at 00:34
  • @Andrey Yup, but one could just search for it without the exact term too. –  Jan 04 '14 at 00:35

1 Answers1

6

Notice that myfloat is a float*. This means that if you write an expression like

myfloat + n

the result will be a pointer to the float that is n positions down in the array of floats whose address starts at myfloat. In other words, adding n here will automatically increment the pointer by n * sizeof(float) bytes, since pointer addition is aware of the size of the object.

Consequently, if you write

myfloat + n * sizeof(float)

you are not jumping forward by n * sizeof(float) bytes, but rather than n * sizeof(float) * sizeof(float) bytes, which happens to be 16n bytes.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065