C Struct Order of Members -


i'm taking on piece of code..c programming in linux. did small change struct

typedef struct {   unsigned int a1;   ..   ..   ..   float f1;   unsigned int a2;   unsigned int a3;    unsigned int offending; // shifted } test; 

i shifted unsigned int offending before float f1, this:

typedef struct {   unsigned int a1;   ..   ..   ..   unsigned int offending;   float f1;   unsigned int a2;   unsigned int a3;   } test; 

and code crashes... problem?

is order of members of c struct important?

what problem? depend on rest of code, , else did.

no, order of members of struct not intrinsically important. made when other code depends on it.

possible causes (not exhaustive):

  • you didn't recompile , there external linkage on struct or aspect of it.
  • by moving member changed alignment of other members and/or sizeof() struct, , didn't compensate that.
  • there literal constant or macro somewhere size or offset depends on struct.
  • there faulty code never failed before because of change in memory layout.
  • the struct used somewhere part of struct or union, , problem related that.
  • there list initialisation using {} no longer matches member order.

you should provide details of how crashes. otherwise guesswork. , perhaps then.

edit: ht @jens.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -