Consider the following code:
struct CExample { int a; } int main(int argc, char* argv[]) { CExample ce1; CExample ce2; cout << "Size:" << sizeof(ce1) << " Address: " << &ce1 << endl; cout << "Size:" << sizeof(ce2) << " Address: " << &ce2 << endl; CExample ceArr[2]; cout << "Size:" << sizeof(ceArr[0])<< " Address: "<< &ceArr[0] <<endl; cout << "Size:" << sizeof(ceArr[1])<< " Address: "<< &ceArr[1] <<endl; return 0; } Output example:
ce1: Size=4, Address: 0039FAA0
ce2: Size=4, Address: 0039FA94
ceArr[0]: Size=4, Address: 0039FA84
ceArr[1]: Size=4, Address: 0039FA88
With the code there is a 12-byte between the addresses of the first two objects (ce1 and ce2) but there is only a 4-byte difference between the objects in the array.
I thought data-alignment would have something to do with the issue, but I'm still stumped. Any idea what is actually going on here?