I suppose I could treat the sizeof(my_structure) of memory starting on
the structure's address as a character buffer and check if every byte
of it is zero.
That strategy won't work, for at least two reasons:
1) The behavior of treating a pointer to a structure as if it were a
pointer to a character is implementation-defined. I don't think that there
is any place in the standard that requires the implementation to give you
the actual contents of that memory, even if you ask. The only guarantee is
that if you interpret a structure as a character string, and then turn that
same string back into a structure, you get the same results.
2) There is no guarantee that even the occupied bytes of a static
structure will be initialized to zero. For example:
struct X {
float f;
};
X x;
I'm assuming here that x is a static object. Now there is nothing in the
standard that requires a machine's floating-point representation to have all
of its bits zero--but the implementation is obliged to do whatever it takes
to give x.f an initial value of zero.
By implication, if you sere to interpret x as a sequence of characters,
there is no assurance that those characters will be zero. Why should there
be any corresponding assurance for chracters that are not part of a data
member?
If this second example is unconvincing, consider this one:
struct Y {
public: virtual void foo() { }
};
Y y;
I am not aware of *any* C++ implementation that will store y as a value with
all of its bits zero.