|
-
Jul 21st, 2005, 01:04 AM
#1
Thread Starter
PowerPoster
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...
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Jul 21st, 2005, 01:11 AM
#2
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?
-
Jul 21st, 2005, 02:00 AM
#3
Thread Starter
PowerPoster
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.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Jul 21st, 2005, 02:57 AM
#4
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.
- ØØ -
-
Jul 21st, 2005, 01:47 PM
#5
Thread Starter
PowerPoster
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.)
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Jul 21st, 2005, 11:32 PM
#6
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
-
Jul 22nd, 2005, 07:31 AM
#7
Re: constructor not firing with malloc...
Placement new, to be precise:
Code:
void *mem = malloc(sizeof(TheClass));
TheClass *ptr = new (mem) TheClass(bla);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|