> This is because of the alignment that the C compiler forces on the structure.
> Basically, it may require that a long _should_ start at the beginning of a
> word, which is 4 bytes in linux. So there are two pad bytes added to the end
> of the 2 byte array.

Ya, I figured that out. In DOS, it works alright because a word is 2
bytes, but on 32 bit systems it is 4 bytes. The alignment to word
boundaries is basically an optimisation thing. It speeds up access at the
expense of more memory usage.  The problem is that I cannot find any
options to change this.  In BC there were flags that allowed you to
specify all optimisations, but I can't find all of them on gcc.  There are
options for the Intel 80960 family, but none for the x86 family.

> There might be a gcc extension to force this, but I am not aware of any. In
> any case, code which needs to tell the compiler to change its alignment rules
> need to be rethought.

Couldn't find anything.

> If you use this structure to fread()/fwrite(), you don't have to worry about
> the alignment - that is totally internal to the compiler, and transparent from
> the user.

The problem is that that does not happen.  I am trying to read the header
of a bitmap.  This is 14 bytes long.  The correct way to use fread is to
specify sizeof(struct) as the size of the data element to be read.  sizeof
returns 16 bytes.  fread reads 16 bytes. The first two bytes go into the
correct locations.  The next two bytes go into the two padding bytes, and
the remaining 12 bytes go into the rest of the struct.  This is a problem
because:
a. I have two bytes from the information header that I should not have
b. The entire file header save the first two bytes is shifted two bytes to
the left.  So I have the wrong bytes in the wron data items.

In other words.  this is not what should happen.  The compiler should have
taken care of it.  I have tested it.  The long Size variable should be
154260 or something like that.  It contains 2.

Philip


To subscribe / unsubscribe goto the site www.ilug-bom.org ., click on the mailing list 
button and fill the appropriate information 
and submit. For any other queries contact the ML maintener

Reply via email to