delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/12/01:09:06

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Why does sizeof give me...
Date: Wed, 06 Aug 1997 22:17:00 -0400
Organization: Cornell University http://www.cornell.edu
Lines: 64
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <33E9301B.AC3@cornell.edu>
References: <33E957BE DOT 7216 AT geocities DOT com> <Pine DOT SUN DOT 3 DOT 93 DOT 970806213245 DOT 6848B-100000 AT degobah>
Reply-To: asu1 AT cornell DOT edu
NNTP-Posting-Host: cu-dialup-0038.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Insomnia wrote:
> 
>         At least part of your problem is most likely due to gcc's 
> behavior of 32-bit alligning fields in a structure.

that is correct but a large part should be because int's are 32 bit
rather than 16.

>   struct mystruct {
>     char element1 __attribute__ ((packed));
>     int  element2 __attribute__ ((packed));
>     .....
>     };
>         Hope this helps.  Corrections/constructive criticism welcome, 
> as are flames

not a flame. in declaring structs as packed, you do not have to apply
the attribute to each element of the struct, you can apply it to the
struct as a whole:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 typedef struct a
 {
   short x;
   int   y;
 } a;

 struct p
 {
  short x;
  int y;
 } __attribute__((packed)) p;

 printf("%lu\n", sizeof(a));
 printf("%lu\n", sizeof(p));

return 0;
}

output:

D:\djgpp\C>test
8
6





-- 
   Sinan
 
*******************************************************************
 A. Sinan Unur                                WWWWWW
                                              |--O+O
 mailto:sinan DOT unur AT cornell DOT edu                C   ^ 
 http://www.people.cornell.edu/pages/asu1/     \  ~/ 

Unsolicited e-mail is _not_ welcome, and will be billed for.
*******************************************************************

- Raw text -


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