www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/11/06/08:49:06

From: Karl Heinz Buchegger <kbuchegg AT gascad DOT at>
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,comp.os.msdos.djgpp
Subject: Re: Holes in structure
Date: Tue, 06 Nov 2001 14:39:29 +0100
Organization: GASCAD
Lines: 48
Message-ID: <3BE7E811.D5D7BA72@gascad.at>
References: <3BE7D280 DOT C1732E58 AT bigfoot DOT com>
NNTP-Posting-Host: 62.46.206.5
Mime-Version: 1.0
X-Trace: fu-berlin.de 1005053918 35627882 62.46.206.5 (16 [95537])
X-Mailer: Mozilla 4.5 [en] (WinNT; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com


Alex Vinokur wrote:
> 
> Questions.
>  1. struct type0 (empty) :
>            Why is 'sizeof' = 1?

Because the standard says so. Even an empty struct must have
at least a sizeof of 1. Otherwise you could not create
arrays of such structs, because in an array every element
must have a distinct address. With a sizeof of 0, all array
elements would consume the same memory location.

> 
>  2. struct type1 :
>            Why is not 'Diff' (between 'sizeof' and 'All items sum')
>   equal 'All holes sum' ?
>      Note. 'Diff' = 9, 'All holes sum' = 6.
>     Where has 3 bytes ('Diff' - 'All holes sum') gone?

The compiler had to pad the whole structure from (43+6)49 Bytes
with 3 additional Bytes to reach the next number (52) which is divisable
by for. He did it to meet address requirements on your cpu for maximum
speed in accessing the struct.

Imagine you create an array of these structs. Each and every array
element has to have an address which is divisable by 4 (on your CPU).
With a total struct size of 49 this is impossible. Thus the compiler
enlarged the struct to have 52 Bytes.

> 
>  3. struct type0 and type1 :
>            Why is not ('Diff' - 'All holes sum') the same value for
> type0 and type1.
>            Note. type0  : 'Diff' - 'All holes sum' = 1 - 0 = 1
>                  type1  : 'Diff' - 'All holes sum' = 9 - 6 = 3
> 

because it is dependent on the actual struct size. If the compiler
decides to pad the struct itself to the next boundary (multiple
of 4 in your case) he does so. Obviously the compiler didn't pad
struct type0.


-- 
Karl Heinz Buchegger
kbuchegg AT gascad DOT at

- Raw text -


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