www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/11/13:52:52

Xref: news2.mv.net comp.os.msdos.djgpp:2616
From: terra AT diku DOT dk (Morten Welinder)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: SIZEOF disparity...?
Date: 10 Apr 1996 22:06:51 GMT
Organization: Department of Computer Science, U of Copenhagen
Lines: 41
Sender: terra AT tyr DOT diku DOT dk
Message-ID: <4khbdr$1on@vidar.diku.dk>
References: <Pine DOT A32 DOT 3 DOT 92 DOT 960406153743 DOT 75628A-100000 AT acs5 DOT acs DOT ucalgary DOT ca>
NNTP-Posting-Host: tyr.diku.dk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Mike <mbwarren AT acs DOT ucalgary DOT ca> writes:

>what am i doing wrong:

>with djgpp v2, when i take the sizeof this struct:

>typedef struct
>{
>  char magic[2];
>  long size;
>  int dummy;
>  long offset;
>} header_t;

>it returns 16, when it should return 14 (2 chars = 2 + long (4) + int (4)
>+ lont (4) = 2+4+4+4=14)


This argument is incorrect.  The size of the struct should be

	2 + P1 + 4 + P2 + 4 + P3 + 4 + P4

where P1,...,P4 are arbitrary non-negative integers.  (This, of
course, assuming that longs and ints are fours bytes each.)

In short: the C standard does not define the memory layout of
a struct very precisely, but explicitly leaves it to the
implementation.  For speed, djgpp aligns modulo 4.  (It could
even be argued that two consequtive compiles needn't chose
the same layout, but that's not likely.)

Check the manual and the FAQ for ways of overriding this.


>(BTW, why are int's represented as 4 bytes on a PC? sizeof(int) returns
>4...)

Well, 2-byte-integers are too small for confort; 8-byte-integers are
rarely needed.  Four is such a nice number.

Morten

- Raw text -


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