Reputation: 1987
I'm taking over a piece of code..c programming in linux. I did a small change to a struct
typedef struct {
unsigned int a1;
..
..
..
float f1;
unsigned int a2;
unsigned int a3;
unsigned int offending; // shifted this
} test;
I shifted unsigned int offending to before float f1, like this:
typedef struct {
unsigned int a1;
..
..
..
unsigned int offending;
float f1;
unsigned int a2;
unsigned int a3;
} test;
and the code crashes... what could be the problem?
Is the order of members of a c struct important?
Upvotes: 0
Views: 402
Reputation: 10863
What could be the problem? Depend on the rest of the code, and what else you did.
No, the order of members of a struct is not intrinsically important. It is made so when other code depends on it.
Possible causes (not exhaustive):
You really should provide details of how it crashes. Otherwise it guesswork. And perhaps even then.
edit: ht @Jens.
Upvotes: 3
Reputation: 79003
The most probable reason for crashes if you change data layout is initialization. If you have old-time initializers in your code that use declaration order, all of a sudden the fields will receive different values than before. Therefore modern C since C99 has designated initializers that avoid that problem:
test toto = { 32, ... , 42, }; // sensible to reordering
test tata = { .a1 = 32, ... , .offending = 42, }; // still the same
Upvotes: 1