delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/07/26/22:18:33

Xref: news-dnh.mv.net comp.os.msdos.djgpp:1182
Path: news-dnh.mv.net!mv!news.sprintlink.net!gatech!news.uoregon.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!EU.net!Austria.EU.net!newsfeed.ACO.net!news.iif.hu!news.bme.hu!scsing.switch.ch!news.belwue.de!news.uni-stuttgart.de!rz.uni-karlsruhe.de!xlink.net!news.asys-h.de!asysha.asys-h.de!not-for-mail
From: ilse AT asysha DOT asys-h DOT de (Juergen Ilse)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GCC/DJGPP Weirdness?
Date: 26 Jul 1995 17:21:50 GMT
Organization: Advanced Systems Software GmbH
Lines: 62
References: <3v1t91$bjs AT alpha DOT epas DOT utoronto DOT ca>
Nntp-Posting-Host: asysha.asys-h.de
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

Hallo,

David Scully (dscully AT blues DOT uucp) wrote:
: Hi All;
: 
: I'm new to DJGPP and have only programmed in Borland C before now.
: I've come across some unusual behavior in GCC while attempting to compile
: a program that I had written in Borland C.  Part of my program
: involved reading a Windows BMP off of disk and I had set up a structure
: to read the BMP header into.
: 
: struct { unsigned short label;
:          unsigned long total_bytes;
:          unsigned long blank1;
:          unsigned long first_pixel;
:          unsigned long infosize;
:          unsigned long width;
:          unsigned long height;
:          unsigned short planes;
:          unsigned short bits_pixel;
:          unsigned long compression;
:          unsigned long size;
:          unsigned long blank2;
:          unsigned long blank3;
:          unsigned long colours_used;
:          unsigned long colours_important;
:         } bmp_header;
: 
: Then I used:
:         fread(&bmp_header,54,1,in_file);
: to read the structure from the disk.  Now for some reason this doesn't
: work with DJGPP, the bytes don't end up being aligned properly after the
: read.  I discovered that  I sizeof() on the structure reveals that it is
: 56 bytes long instead of 54 as declared.
:    My question is am I trying to do something non-standard that Borland
: allowed me to get away with or is this an actual DJGPP/GCC bug and if the
: latter does anyone know a work around for this.
: 
: 
:         Thanks in advance,
: 
:                           Neil Torrie
: 
: 
: P.S.  This is a borrowed account please answer in this news group and not
:       by E-MAIL.

....the answer to your question is that you made something non-standard!
GCC has done some alignement (which is legal for a C-compiler) to achive higher
performance. It has aligned the structuremember total_bytes to longword-bounda-
ry so that you got 56 instead of 54 for the size of this structure. To make
your program portable you shouldn't read in the complete structure at once,
because the compiler can do some padding to align structureelements. To be
really portable you should read the structure byte by byte, because on non-
Intel-architectures you could have stored short integers and long integers in
a different way (for example 68000 CPU's: High byte first).

hope that helps...
cia
		Juergen Ilse									(ilse AT asys-h DOT de)


- Raw text -


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