Removing bit fields in a structure
I have a question. I have a legacy application that uses bit fields in a struct. Something like that
struct sample
{
BYTE one: 2;
BYTE two : 1;
BYTE three: 5;
} sampletest;
So that three
could be the value MAX
31.
Now I have a requirement to increase the value MAX
three
. I am planning on removing the bit fields. It will now handle more memory. Other than that, is there anything I need to take care of? Does this mean other harm?
You need to carefully examine the entire code to check if the code is using this structure containing bit fields.
For example, it may be that somewhere in the code, an entire byte can be read, processed and written back (this is not a problem with casts) - if you remove bit fields, this code breaks. So look for casts - they are code indicator to check.
source to share
There are a few additional things that may be important to you. Or not.
In C, bit bit fields can only be declared as "int", "signed int", or "unsigned int" (also "bool" in C99). Any other type is not allowed (or not portable if your compiler allows it as an extension). (I wonder what is behind "BYTE" in your case.)
In addition, the type 'int' (not explicitly 'signed' or 'unsigned'), if used in a bitfield declaration, can declare either a signed or unsigned field, depending on the implementation (and / or its compilation options).
Since you're planning on converting your "three" to a regular field (not a bit), it might make sense to check if it should be signed or unsigned.
source to share
As announced, assuming #define BYTE unsigned char
in a header file somewhere, your structure only takes one byte if the compiler packs the fields nicely. If the code only accesses the fields within the struct and never tries to copy / null the struct as a whole, assuming it only has one byte (for example sizeof struct sampletest
), you will be fine. It should also be a fairly straightforward way to dig up the code and examine every place it touched just to make sure, let alone run the system through your test loop after you've made the changes.
source to share