I have inherited some code that makes use of bitfields in a struct:
typedef _my_flags {
unsigned int x_ida:1;
unsigned int x_foo:6;
unsigned int x_bar:6;
unsigned int x_bonzo:6;
unsigned int x_pizza:6;
unsigned int x_jack:1;
unsigned int x_flashed:1;
unsigned int x_flabberghasted:1;
} t_my_flags;
typdef _my_struct {
short cat;
int foo, bar, bla;
t_my_flags flags; /* ... */
char* name;
}
Both structures are part of the public API.
Now i'm currently in dire need of adding some additional flags (well, actually only one), so I was wondering whether it is safe to add extend the t_my_flags
struct:
typedef _my_flags {
unsigned int x_ida:1;
unsigned int x_foo:6;
unsigned int x_bar:6;
unsigned int x_bonzo:6;
unsigned int x_pizza:6;
unsigned int x_jack:1;
unsigned int x_flashed:1;
unsigned int x_flabberghasted:1;
unsigned int x_ready:1; /* new member */
} t_my_flags;
EDIT these structs are used in a dynamic library (so don't worry about persisting the data on a filesystem or sending it over a network)
I'm worried about breaking binary-compatibility.
My change will grow the t_my_flags
struct from 28bit to 29bit, so I assume that it will live in a 32bit integer anyhow.
So the total size of the structs is not going to change.
OTOH, I don't know whether the ordering of the flags-fields is going to change...
Of course, this ought to run on a variety of architectures (x86_64, i386, arm32, arm64, s390x, ppc) on all major OSs (Linux, macOS, Windows) with unknown compilers (obviously gcc/clang and MSVC, dunno about others; we stick to C89 for a reason...).
EDIT We can probably assume that on any given OS/architecture the same compiler is used (well, for Windows, we use gcc's -mms-bitfields
flag which should give us bitfield compatibility with MSVC).
The architecture/OS/compiler list given above is merely to indicate that I'm interested in how my use-case behaves in different environments. I'm less concerned about the interoperability between different environments).
So: how save is adding a new bitfield without changing the overall size in terms of binary compatibility?