TITLE: how to zero non-static data PROBLEM: euahjn@eua.ericsson.se (Henrik Johansson) There is an effective way to emulate the behaviour of good old calloc() to zero the contents of a class. Overload the "operator new()" procedure, and put the memset(xx, 0, sizeof(yy)) in it. class DerivedClassX: public BaseClass1 { public: void *operator new(size_t s); } #include // memset() void *DerivedClassX::operator new (size_t s) { void*chunk = ::operator new(s); memset (chunk, 0, sizeof(DerivedClassX)); return chunk; } When calling "new DerivedClassX", the standard operator new() is called, then the memory will be zeroed, and then any constructor is called. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 1 Nov 95 This is a good idea, but one point of caution: If a class is derived from DerivedClassX, the operator new is inherited. In that case MoreDerived* p = new MoreDerived; will result in a call to DerivedClassX::operator new with a value of sizeof(MoreDerived), but the memset will zero only some of the bytes at the beginning of that memory area. This might or might not correspond exactly to the location of the DerivedClassX part of the chunk. In any case, the derived-class data will probably not be zeroed, which might be surprising to users of MoreDerived. It would probably be best to use the requested size as the argument to memset. You can read more about writing your own memory allocators in the presence of inheritance in "Effective C++" by Scott Meyers. The original question asked about classes with lots of integer members needing to be set to zero. All-bits-zero will always be correct for integer types. Someone else correctly pointed out that you should not rely on all-bits- zero being correct for other data types. Null pointers and floating-point zeroes in particular might not be represented as all bits zero. (Writing a 0 in C++ source code doesn't mean the representation in the translated program consists only of zero bits.) [ Interesting idea, but due to limitations should be used for special cases, in my opinion. For example, if you allocate a DerivedClassX as a local on the stack, its data members will not be zeroed. Also you should not rely on this as a general mechanism for initialization. You should explicitly initialize all data members. -adc ]