S
Simon Elliott
I have ome new code which has to work with some legacy code which does
a lot of memset's and memcmp's on structs of PODs. This leads me to
want to do stuff like this:
struct foo
{
unsigned char c1_;
unsigned short us1_;
};
std::vector<foo> fooVect;
memset(&foo, 0, sizeof(foo));
fooVect.push_back(foo);
The compiler I've tested this with uses 4 byte alignment by default and
hence puts in some padding bytes. Looking at the copy of foo which is
now at fooVect[0], the padding bytes have random values. The zero
setting of the padding bytes has not been preserved when foo has been
copied to the vector.
I'd expect that what happens to padding bytes is undefined, but it
would be useful to have them preserved as zero. Any sensible,
non-kludgy, portable way of doing this?
a lot of memset's and memcmp's on structs of PODs. This leads me to
want to do stuff like this:
struct foo
{
unsigned char c1_;
unsigned short us1_;
};
std::vector<foo> fooVect;
memset(&foo, 0, sizeof(foo));
fooVect.push_back(foo);
The compiler I've tested this with uses 4 byte alignment by default and
hence puts in some padding bytes. Looking at the copy of foo which is
now at fooVect[0], the padding bytes have random values. The zero
setting of the padding bytes has not been preserved when foo has been
copied to the vector.
I'd expect that what happens to padding bytes is undefined, but it
would be useful to have them preserved as zero. Any sensible,
non-kludgy, portable way of doing this?