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 22:01:30
|
Footnote: Try placing numVert last in the struct. Then all items in the
struct will be correctly aligned, and a single pad byte at the end should
suffice to make the struct 24 bytes large.
/Per W
On Sat, 12 Mar 2005, Per Westermark wrote:
> 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!!!
>
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|