constructor not firing with malloc...
I'm not sure whats up, but maybe this is another suttle thing that has slipped by without causing a problem...
It seems I have an array of objects which do not call there constructors when the array is resized...
The objects contains class' which NEED there constructors called...
Re: constructor not firing with malloc...
Forget me if I'm wrong, but I was under the impression the constructor function is only called when using the new operator?
Re: constructor not firing with malloc...
no, it definetly calls any time an object is instantiated...
I figured that would reach out to malloc or new calls.
Re: constructor not firing with malloc...
Nope, penegate is right. New and Delete is the C++ way, where you have classes and objects, that can contain constructors, destructors.
Malloc and Free is the C way with no classes and object, hence no constructor and destructor call either.
- ØØ -
Re: constructor not firing with malloc...
bah!
Well my array class has to use malloc/realloc since it has resize memory a lot faster than new can (resize using new = temp memory, expand, copy temp to new memory, delete temp... realloc = resizes current memory and/or moves it all to a new spot.)
Re: constructor not firing with malloc...
Well then after you malloc/realloc you then need to call the constructors manually seeing as that's what new does ;)
Re: constructor not firing with malloc...
Placement new, to be precise:
Code:
void *mem = malloc(sizeof(TheClass));
TheClass *ptr = new (mem) TheClass(bla);