Results 1 to 7 of 7

Thread: constructor not firing with malloc...

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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?

  3. #3

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.


    - ØØ -

  5. #5

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width