Possible Duplicate:
Why does ‘sizeof’ give wrong measurement?
I have a structure called CBUFFER_PEROBJECT:
struct CBUFFER_PEROBJECT
{
D3DXMATRIX Final;
D3DXMATRIX Rotation;
};
And in another class I do this:
...
bd.ByteWidth = sizeof(CBUFFER_PEROBJECT);
...
I found out that the size of D3DXMATRIX is 64, so 64+64 = 128 (right?). But my compiler is playing tricks with me (Visual C++), because as I was debugging the program, the bd.ByteWidth became 132, so I went to the Immediate Window (Visual Studio), and typed:
sizeof(D3DXMATRIX) + sizeof(D3DXMATRIX)
And the result was:
128
But the bd.ByteWidth became 132, and when I type the following into the “Immediate Window”:
sizeof(CBUFFER_PEROBJECT)
It gives me:
128
Sometimes when compilers are evaluating structure declarations, they will add bytes of padding between fields. This is because some types perform better when aligned to memory addresses that are multiples of their length.
I’d go so far as to say it’s very likely that you would not see this effect in the immediate window.
Given these grounds, this is a duplicate question. You can get a more in-depth answer here: Why isn't sizeof for a struct equal to the sum of sizeof of each member?