Mail Archives: djgpp/2000/05/09/11:09:57
pad2369 wrote:
>> A `char' takes a single byte.
> 
>I am not an expert about gcc internals, so I sould say 
>something silly here: do you mean that this function 
>allocates only two bytes of stack for auto variables?
>
>void func(void)
>{
>char a, b;
>    ...
>}
It allocates only 1 byte per char.  However, in order to
maintain stack alignment it must always allocate a multiple of
4 bytes.  So in this case it will allocate 4 bytes.  In general,
n chars will result in 4*((n+3)/4) bytes being allocated (where /
denotes integer division with rounding towards zero).
>I thought gcc aligned to 32 bit words auto variables 
>for efficiency reasons, like with struct members, thus 
>allocating 8 bytes in the above example. 
Single bytes can be accessed efficiently in any position,
because wherever you put them they never cross a boundary.
>struct char_ab {
>  char a, b;
>};
sizeof(struct char_ab) == 2
>void func(void)
>{
>struct char_ab ab;
>    ...
>}
Again, 4 bytes are allocated in this case, in order to keep the
stack aligned.
S.
- Raw text -