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

From: "Peter Nilsson" <airia AT acay DOT com DOT au>
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,comp.os.msdos.djgpp
References: <3BE7D280 DOT C1732E58 AT bigfoot DOT com>
Subject: Re: Holes in structure
Date: Wed, 7 Nov 2001 00:25:34 +1100
Lines: 87
X-Newsreader: Microsoft Outlook Express 4.72.3110.1
X-MIMEOLE: Produced By Microsoft MimeOLE V4.72.3110.3
NNTP-Posting-Host: acay02721190.acay.com.au
Message-ID: <3be7e3fb@news.rivernet.com.au>
X-Trace: news.rivernet.com.au 1005052923 acay02721190.acay.com.au (7 Nov 2001 00:22:03 +1000)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Perhaps a post best left with comp.os.msdos.djgpp


Alex Vinokur wrote in message <3BE7D280 DOT C1732E58 AT bigfoot DOT com>...
>[program to scan structs]

>struct type0
>{
>};

>struct type1
>{
>  long   item1;
>  char   item2;
>  short  item3;
>  char   item4;
>  long   item5;
>  long   item6;
>  int    item7;
>  short  item8;
>  short  item9;
>  char   item10;
>  char   item11;
>  long   item12;
>  float  item13;
>  double item14;
>  char   item15;
>};

>[platform dependant output]

>Struct type1 :
> sizeof   = 52
> All items sum  = 43
> Diff (52 - 43)  = 9
> All holes sum  = 6
>
>Struct type0 :
> sizeof   = 1
> All items sum  = 0
> Diff (1 - 0)  = 1
> All holes sum  = 0


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

Becuase in an array of structs, even empty structs, each element must have a
unique address in order to insure that pointer expressions have meaning.


> 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 difference is the padding at the end of the struct. [Hole _after_ last
element.]


>
> 3. struct type0 and type1 :
>           Why is not ('Diff' - 'All holes sum') the same value for
>type0 and type1.

Becuase type1 contains elements of sizeof == 4.  Presumably, on the given
system, these need to be aligned on 4 byte boundaries.  Consequently the
type1 struct must align on a 4 byte boundary, consequently 3 bytes are
needed after the last char.

The sizeof type0 is explained above.


>           Note. type0  : 'Diff' - 'All holes sum' = 1 - 0 = 1
>                 type1  : 'Diff' - 'All holes sum' = 9 - 6 = 3

Incidental.


HTH

--
Peter


- Raw text -


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