I have a struct with an unsigned char[16]
field that I'd like to initialize to zeros. The following (strongly simplified) code compiles fine with clang (OS X):
struct GUID {
unsigned char bytes[16];
GUID() : bytes("\0\0\0\0""\0\0\0\0""\0\0\0\0""\0\0\0") {};
}
Note I use 15 \0
s because the 16th is the zero terminator of the string literal, and clang complains if you initialize a string with too many bytes.
Now, when I try to compile with GCC 4.5.3 (cygwin), I get:
error: incompatible types in assignment of 'const char [16]' to 'unsigned char [16]'
Why doesn't it work, and how can I make it work? (I could obviously loop through the array in the constructor, but I'd like to use the initialization list if possible, and I'd like to understand why it works in one compiler, but not the other.)
A simple bytes()
should be sufficient in this case. You could also consider bytes { 0,0,0,0.... }
.
Also, use std::array
, not T[]
. Only fools use T[]
when they could use std::array
.
Since we're dealing with POD its sufficient to explicitly construct bytes, which will result in the corresponding memory being 'zeroed':
struct GUID
{
unsigned char bytes[16];
GUID() : bytes(){}
};
It's probably worth noting that if you didn't explicitly construct bytes in the initialization list, it would be left uninitialized
struct GUID
{
unsigned char bytes[16];
GUID(){};
};
If the member variable were not a POD but instead a member object then instead of being left uninitialized it would call its default constructor.
In the GUID constructor you can use memset(bytes, 0, sizeof(bytes));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With