delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/03/08:39:21

Sender: Viktor DOT Stujber AT st DOT fmph DOT uniba DOT sk
Date: Thu, 3 Jul 2003 14:04:47 +0200
From: Viktor DOT Stujber AT st DOT fmph DOT uniba DOT sk
To: DJGPP AT DELORIE DOT COM
Message-ID: <00A224F4.86EC817C.6@st.fmph.uniba.sk>
Subject: data alignment issue

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 -


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