Thursday, May 9, 2013

C structures and memory consumption

Hack to death on your nose: when you declare a structure in C, the bigger data types must go at first. For example:
struct S {
    long double ld;
    char *p; // can be 8 bytes on x64    
    double d;
    float f;
    int i;
    char c;    
} *p;
But not:
struct S {
    char c;
    int i; 
    float f;
    double d;
    char *p;  
    long double ld;
} *p;
I guess the same rule is valid not only for C.

No comments:

Post a Comment