delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/08/12/09:37:47

From: ad AT papyrus DOT hamburg DOT com (Andreas Degert)
Subject: Re: packed structures
12 Aug 1998 09:37:47 -0700 :
Message-ID: <87btpqwnr0.fsf.cygnus.gnu-win32@papyrus.hamburg.com>
References: <000101bdc547$2b785c20$e645aace AT TomStokes>
Mime-Version: 1.0 (generated by tm-edit 7.106)
To: "Tom Stokes" <tstokes AT pacbell DOT net>
Cc: "CYGWIN32" <gnu-win32 AT cygnus DOT com>

"Tom Stokes" <tstokes AT pacbell DOT net> writes:

> The size of this structure is 12 which is wrong. What am I missing?
[...]

try the following (compile and execute or compile to .s):

------------------------------------------
typedef struct HeaderTag 
{
  unsigned long   HeaderLength;
  char            PackingType __attribute__ ((packed));
  unsigned short  NumRows __attribute__ ((packed));
  unsigned short  PackedSize __attribute__ ((packed));
} __attribute__ ((packed)) HeaderType;

#define offset(f) (int)&(((HeaderType*)0)->f)

int main(void)
{
  printf("%d,%d,%d,%d\n",offset(PackingType),offset(NumRows),
	 offset(PackedSize),sizeof(HeaderType));
  return 0;
}
--------------------------------------------

Notice the added attribute after the closing brace. Without that the
whole structure is padded with 3 additional bytes.

ciao

Andreas
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


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