delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/07/26/04:43:11

Xref: news-dnh.mv.net comp.os.msdos.djgpp:1156
Path: news-dnh.mv.net!mv!news.sprintlink.net!cs.utexas.edu!utnut!torn!news.bc.net!unixg.ubc.ca!info.ucla.edu!csulb.edu!csus.edu!news.ucdavis.edu!wheel!elric
From: elric AT wheel DOT dcn DOT davis DOT ca DOT us (Jeffrey Taylor)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GCC/DJGPP Weirdness?
Date: 25 Jul 1995 16:58:49 GMT
Organization: Davis Community Network - Davis, California, USA
Lines: 83
References: <3v1t91$bjs AT alpha DOT epas DOT utoronto DOT ca>
Nntp-Posting-Host: wheel.dcn.davis.ca.us
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

It is very tempting and looks like good programming practice to use 
structs to read data structures from disk.  Because there is no standard 
on how structures are laid out in memory, this is non-portable.  It is 
ugly, it may be slow, but byte by byte is the only portable way to do 
it.  Actually for speed, I read the whole structure into a byte array and 
convert it to ints and longs myself.

For 16-bit words stored most significant byte first:

unsigned short msb1st(unsigned char **p)
{
   unsigned short tmp;

   tmp = *(*p)++ << 8;
   tmp |= *(*p)++;

   return tmp;
}

....

   unsigned char array[10];
   unsigned char a = array;
   unsigned short first, second, fourth;
   unsigned char third;

  first = msb1st(&a);
  second = msb1st(&a);
  third = *a++;
  fourth = msb1st(&a);

No problems with alignment, byte order, etc.


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.

--
=====================================
Jeff T       "Blues with a feeling...
=====================================

- Raw text -


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