Mail Archives: djgpp/2003/07/03/08:39:21
I've noticed that djgpp aligns all members of a structure to 4 bytes.
---------------------------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
----------------------------------------------------
| a _ _ _ b b b b c c _ _ |
----------------------------------------------------
this is the layout of this structure in memory:
struct {
BYTE a;
DWORD b;
WORD c;
}
because of the 4byte alignment, sizeof() returns 12 instead of 7.
Imagine the mess that it causes in this example:
STR str; void *ptr;
ptr=&str;
ptr=malloc...; fread(ptr, 7, 1, file);
guess what (STR *)ptr->a would print...
that's right, the data from the first byte and the next 3 bytes together!
the member a is automatically treated as a DWORD.
Can someone tell me a:
1) compiler switch
2) keyword
3) method
with which I could correct the error?
reply to 2stujber AT st DOT fmph DOT uniba DOT sk, thanks!
- Raw text -