delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/09/11:09:57

From: dontmailme AT iname DOT com (Steamer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Datatype sizes
Date: Tue, 09 May 2000 13:29:58 GMT
Organization: always disorganized
Lines: 43
Message-ID: <391812bf.18389559@news.freeserve.net>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000509120427 DOT 946M-100000 AT is> <200005091019 DOT MAA17313 AT maggiore DOT iperbole DOT bologna DOT it>
NNTP-Posting-Host: modem-193.wisconsin.dialup.pol.co.uk
X-Trace: newsg3.svr.pol.co.uk 957878999 32124 62.137.99.193 (9 May 2000 13:29:59 GMT)
NNTP-Posting-Date: 9 May 2000 13:29:59 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019