I know that memset is frowned upon for class
initialization. For example, something like the following:
class X { public:
X() { memset( this, 0, sizeof(*this) ) ; }
...
} ;
will clobber the vtbl
if there's a virtual
function in the mix.
I'm working on a (humongous) legacy codebase that is C-ish but compiled in C++, so all the members in question are typically POD and require no traditional C++ constructors. C++ usage gradually creeps in (like virtual functions), and this bites the developers that don't realize that memset has these additional C++ teeth.
I'm wondering if there is a C++ safe way to do an initial catch-all zero initialization, that could be followed by specific by-member initialization where zero initialization isn't appropriate?
I find the similar questions memset for initialization in C++, and zeroing derived struct using memset. Both of these have "don't use memset()" answers, but no good alternatives (esp. for large structures potentially containing many many members).
memset(this, 0, sizeof *this)
? (And yes, that would also clobber the vtbl.) – Adulterantthis
from that, then subtract the remainder from the object size. – Traceetracermemset
would mess up as well. Make a choice: write in C (and gut the C++ portions) or write in C++ (and use C++ idioms). Mixing these two things is dangerous and it's probably easier (and superior) to start taking the time to fix these classes one at a time to give them proper constructors, without the C antics. – Consistence