Re: GCC 2.96 shows bogus memory allocation when allocating some given structs?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jul 29, 2004 at 04:28:39PM +0200, Claudio Lavecchia wrote:
> Hello GCC ppl,

Hi Claudio,

> struct S2
> {
>    char        a[6];  
>    int            b;
> };

>    struct S2 s2;
> 
>    printf ("SIZE S2 = %d\n",sizeof(struct S2));
>    printf ("SIZE s2 = %d\n",sizeof(s2));
>    printf ("SIZE s2.a = %d\n",sizeof(s2.a));
>    printf ("SIZE s2.b = %d\n",sizeof(s2.b));

> SIZE S2 = 12
> SIZE s2 = 12
> SIZE s2.a = 6
> SIZE s2.b = 4
> 
> As you can see, size of structure S2 is just pointless..... 

no, it isn't. read on ;)

,--( http://www.plethora.net/~seebs/faqs/c-iaq.html#section-2 )----------------------
|
| 2.8: Why does sizeof report a larger size than I expect for a structure type, as if 
|      there was padding at the end?
|
| Because there's padding at the end. Duh. 
`------------------------------------------------------------------------------------

The long (serious) answer is because usually the struct members are aligned to some
boundary because modern CPUs expect that fundamental types like int, long and float
will be stored in memory at addresses that are multiples of their length. 

CPUs are optimized for accessing memory aligned in this way. 

You can turn this off, if you really need but this is generally not a good idea.

HTH
-- 
Claudio 

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux