artM artM - 3 months ago 9
C Question

bitfields location in struct

Given there are some unrelated flags (

sta_scan
,
search
) that are defined as bitfields in struct like below. Does the location where those flags are declared matter (in terms of memory save)?

struct sta_all {
char name[16];
unsigned int sta_scan:1;
...
int interval;
unsigned int search:1;
}

Answer

Yes, but not always.


Check this example:

#include <stdio.h>
#include <string.h>

struct {
    char name[16];
    unsigned int sta_scan:1;
    int interval;
    unsigned int search:1;
} sta_all;

int main( ) {

   sta_all.interval = 4;
   printf( "Sizeof(sta_all) : %zu\n", sizeof(sta_all) );

   return 0;
}

Output:

Sizeof(sta_all) : 28

and:

#include <stdio.h>
#include <string.h>

struct {
    char name[16];
    unsigned int sta_scan:1;
    unsigned int search:1;
    int interval;
} sta_all;

int main( ) {

   sta_all.interval = 4;
   printf( "Sizeof(sta_all) : %zu\n", sizeof(sta_all) );

   return 0;
}

Output:

Sizeof(sta_all) : 24

This happens because of padding, on my platform.

By the way, if you are really desperate for memory efficiency, and you can accept losing in access speed, then you can use packing, as the link above explains.

Note: The example above confirms what Jonathan Leffler mentioned in this comment:

Each of those bit-fields will probably be allocated as much space as the base type (unsigned int) and will use 1 of the 32 (16, 64, …) bits in that storage unit. If you decide to use bit-fields, you should make sure all the bit-fields are clustered together; it will minimize the space wasted. [...]. In the context of a structure, the compiler has no freedom to move the bitfields around.


Is it worth it?

Not that much IMHO... :)

Comments