Re: [Dev-C++] sizeof returns a wrong value!
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-03-12 21:58:37
|
No, sizeof() does not return the wrong value. What you are forgetting is
that most compilers adds padding when creating structures. Real-life
processors don't like unaligned data because it might require filling of
multiple cache-lines to get access to the data.
Most probably, the compiler created:
0: char numVert;
1: char padding1;
2: unsigned short v1,v2,v3,v4;
10: char padding2[2];
12: float nx,ny,nz;
24: unsigned short matIndex
26: char padding3[2];
The last padding is there to make the complete struct be an even multiples
of 4 bytes, since the largest elemnt in the struct is float, and
sizeof(float)=4.
By making the struct size n*4, the declaration:
struct Face myfaces[10];
will have all 10 structs properly aligned for the float access.
/Per W
On Sat, 12 Mar 2005, The_Eu wrote:
> Hi!
> I have a problem with "sizeof"!
> look in this struct...
> struct Face {
> char numVert; ->1 byte
> short unsigned int v1,v2,v3,v4; ->4*2 bytes = 8bytes
> float nx,ny,nz; ->3*4bytes = 12bytes
> short unsigned int matIndex; ->2bytes
> };
> The total is 23Bytes, but sizeof(Face) indicate 28bytes!!!
> My declaration "short unsigned int", makes a "int" of 4 bytes, not 2 bytes!
>
>
> I'm use Dev C++ 4.9.9.2!
> Whati is this!
> Help!!!
|