I have this typedef:
typedef union
{
unsigned Value;
unsigned Timestamp:16;
} BITFIELD;
and get this compiler warning:
BITFIELD bitfield;
// read from uninitialised memory - may result in unexpected behaviour
bitfield.Timestamp = 12;
Now, the warning disappears when I use a short instead of the bitfield:
typedef union
{
unsigned Value;
unsigned short Timestamp;
} DATATYPE;
I am not sure what to think about this warning – I don’t understand it. There is no uninitialised memory involved and no read operation, too. IMHO the compiler (VisualDSP++ 5.0 C/C++ Compiler) is wrong here. The warning disappears also, when I use a :32 bitfield for Timestamp.
Is there anything I didn’t realize? Can I safely ignore this warning?
How big is an
unsigned inton your system?The only thing I can think of that may cause this is if the 16 bit bitfield forms only part of the
Timestampvariable (if, for example, anunsigned intis 32 bits wide).In other words, maybe the compiler is turning that into:
which would cause that problem if
Timestampwere uninitialised.That fits in with:
Timestamp = 12;.:32since, it expands the bitfield to the point where a direct assignment is also possible.Pure supposition on my part but, discounting a brain-dead compiler, that’s the best I can come up with (if it’s true, it’s probably still brain-dead but in a different way).
FWIW, gcc doesn’t complain even with
-Wall.