Mail Archives: djgpp/1995/07/26/14:23:27
In <3v1t91$bjs AT alpha DOT epas DOT utoronto DOT ca> dscully AT blues DOT uucp (David Scully) writes:
>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 declaration deleted ...)
>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.
DJGPP is a 32-bit compiler, and has a habit of aligning data on word
boundaries (presumably this is faster to access). As a result, the bytes
you define in your struct are not stored in successive memory slots, but
could have "gaps" between them.
There are 2 options :
1) Read in each element of the struct individually (slow!).
2) Tell GCC not to pad the data.
To do this you need to use the "attribute" option for the declaration.
I don't have the source code to hand, but from memory the syntax is:
(example struct) :
struct test {
char x __attribute__ ((PACKED));
int y __attribute__ ((PACKED));
/* etc. for all data in the struct */
};
This ensures that all the elements of the struct are stored contiguously,
so a fread() should word OK.
Hope this long explanation helps! (and isn't wrong!)
Adios
Mark.
--
-------------------------------------------------------------------------
Mark Wodrich /\/\.\/\/.
Electrical Engineering Student, "The truth is out there"
- Raw text -