Possible Duplicate:
Pointer arithmetics in C++ uses sizeof(type) incremention instead of byte incremention?
Tried to find the answer but could not.
I have the following c++ code:
struct A {
uint16_t a;
uint16_t b;
uint16_t c;
}
int main() {
uint16_t * B = new uint16_t[5000];
for (int i=0;i<5000;i++)
B[i] = i;
uint16_t * D = &B[500]+sizeof(A);
}
Question is: why *D=512 after this? When I debug sizeof(A) = 6 as it should be. Yet I add to memory address of B[500] the size of A it adds 12 instead of 6.
I am working with memory address still there is a multiplication and I cannot figure out why it is so.
Please advise.
Thank you!
It’s a compiler bug if
sizeof(A)is indeed 6.and thus
*Dought to be 506 then. And it is when I compilewith g++-4.7.1 or clang++-3.1.