www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/09/05:07:55

Xref: news2.mv.net comp.os.msdos.djgpp:2552
From: fnunez AT cs DOT uct DOT ac DOT za (Fabian Nunez)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: SIZEOF disparity...?
Date: 9 Apr 1996 07:48:06 GMT
Organization: University of Cape Town
Lines: 67
Message-ID: <4kd4nm$1je@groa.uct.ac.za>
References: <Pine DOT A32 DOT 3 DOT 92 DOT 960406153743 DOT 75628A-100000 AT acs5 DOT acs DOT ucalgary DOT ca>
NNTP-Posting-Host: unagi.cs.uct.ac.za
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In <Pine DOT A32 DOT 3 DOT 92 DOT 960406153743 DOT 75628A-100000 AT acs5 DOT acs DOT ucalgary DOT ca> Mike <mbwarren AT acs DOT ucalgary DOT ca> writes:

Hi Mike

>what am i doing wrong:

nothing :)

>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)

In DJGPP (and other C compilers I've used) items in a struct are aligned
on a 4-byte boundary for performance reasons. So in reality you have:
typedef struct
{
	char magic[2];
	char padding[2]; /* added by DJGPP, not accessible by your code */
	long size;
	int dummy;
	long offset;
} header_t;

In fact, if you made (say) the "dummy" element a char instead of an int
you'll find sizeof(header_t) is still 14 (DJGPP will insert 3 padding bytes).

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

DJGPP chooses to use dwords for ints. There is a command-line switch to use
16 bit ints, but I can't remember what it's called (it's in the info docs)

>i am using djgpp v2 on a 486-33 w/4megs ram...

>(if this is in the FAQ, sorry, but i can't use www/ftp; our DNS server is
>down...)



>------------.        mailto:mbwarren AT acs DOT ucalgary DOT ca          .--------------
>.___  ___.  |      o         _ |           ?                  |"a smoking
>|   \/   |  |	  /\,	  (*) '\o__        o                  | section in a
>|  i K E |  | .(*)>(*)______'(*)\_____./'\.|\_________.       | resturant is
>|        |  | | Gateway to the    TEMPLE OF MIKE      +---+   | like a peeing
>|__|\/|__|  | `-.----------------------------------.--'   |   | section in a
>w a r r e n |   | http://www.ucalgary.ca/~mbwarren +------+   | swimming pool"
>------------'   `----------------------------------'          '\.____________

Interesting quote - how graphic ;)

Hope that helps

Fabian
--
Fabian Nunez, Bsc(hons) student, University of Cape Town                     
email:fnunez AT cs DOT uct DOT ac DOT za   web:http//www.cs.uct.ac.za/~fnunez
----------------------------------------------------------------             
"k3wl aRe th3 lAmErz, 4 thEy sh4ll RulE!" - from the ElitE Bible 

- Raw text -


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