G
goldfita
I saw some code that appeared to do something similar to this
struct foo {
char offset[XXX];
int d;
};
struct foo {
int a;
int b;
char c;
int d; //same as other d
};
And the structs are defined differently in different files. I tried
doing this myself by just casting pointers to these structs from one
type to the other. I want the members after offset to align, so I
tried setting XXX = sizeof(int)*2+sizeof(char). But the compiler
padded that char with 3 more bytes; so, obviously it didn't work. Then
I tried embedding a struct defined as
struct inner_foo {
int a;
int b;
char c;
};
And I used that inside foo. Then I set XXX to sizeof(inner_foo). And
that worked. So my question is, is that safe and portable? If no,
please make a suggestion. Also, does the compiler always layout the
struct members in the same order you declare them?
thank you
-Todd
struct foo {
char offset[XXX];
int d;
};
struct foo {
int a;
int b;
char c;
int d; //same as other d
};
And the structs are defined differently in different files. I tried
doing this myself by just casting pointers to these structs from one
type to the other. I want the members after offset to align, so I
tried setting XXX = sizeof(int)*2+sizeof(char). But the compiler
padded that char with 3 more bytes; so, obviously it didn't work. Then
I tried embedding a struct defined as
struct inner_foo {
int a;
int b;
char c;
};
And I used that inside foo. Then I set XXX to sizeof(inner_foo). And
that worked. So my question is, is that safe and portable? If no,
please make a suggestion. Also, does the compiler always layout the
struct members in the same order you declare them?
thank you
-Todd