-
March 19th, 2004, 08:41 PM
#1
struct with variable length member
Consider the code below.
Why is the output wrong when i define a3 of struct AAA as a DWORD, but when it is defined as a WORD, output is ok?
#include <windows.h>
#include <stdio.h>
BYTE buffer[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd};
//#pragma pack(2)
typedef struct {
WORD a1;
WORD a2;
DWORD a3; //<-----this gives wrong output
}AAA;
typedef struct {
WORD b1;
AAA b2[1];
} BBB;
BBB*bbb = (BBB*)buffer;
void main() {
//with a3 as a WORD : 2211 4433 6655
//with a3 as a DWORD: 2211 6655 8877 <----this is wrong, why?
printf("%x %x %x", bbb->b1, bbb->b2[0].a1, bbb->b2[0].a2);
}
-
March 20th, 2004, 07:35 AM
#2
Uncomment the #pragma and see if the problem goes away. To me it looks very much like such a problem. You can also output sizeof(BBB) with WORD and DWORD. If the DWORD version is 2 bytes larger than the WORD version, it's weird, otherwise you have a packing problem.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|