Silly Constructor Question.

Started by
6 comments, last by Zahlman 16 years, 5 months ago
Question: Is there a way to call a constructor on memory returned from malloc(...) in C++? Example:

class CA 
{ 
   public: 
      CA() {}; 
      virtual void foo() { printf( "CA\n" ); }; 
};

class CB : public CA 
{ 
   public: 
      CB() {}; 
      virtual void foo() { printf( "CB\n" ); }; 
};
And in the code...

CB *p_cb = (CB*)malloc(sizeof(CB));
// Initialize?
p_cb->foo();
Explanation: Okay, so without some type of initialization, the above program dies a horrible death because it won't initialize the vtables. Now, I know I could just use new which does this, but I'm now curious on "what if" this wasn't an option. This came up between me and a friend discussing mem pools, so anyway, silly example... So, say I want to extend stack memory, so I make a memstack class and in the init I just malloc a chunk of continuous memory. Awesome, I have memory. So it has an offset from the beginning. I have a method "alloc(size_t size)" that will save the current offset, move it by size, and then return the old offset. Now I have a chunk of memory that needs to be initialized... and I have these awesome constructors already, but no idea how to call them on that returned memory. *p_cb = CB() does indeed call the constructor, but doesn't set the vtable... Anyway, sorry if this is stupidly simple... Been playing around with it for an hour and MSDN and google are no help... thought I'd post it here before bed and see if I got an answer by mornin' by the resident guru's. Thanks! PS: Saw something on a site that said you could "suppress" the constructor being called by new... they said nothing more, wish they did... it seems to be the other side of the same coin...
Network EngineerVolition Inc.
Advertisement
You could use inplace new:
new( malloced_pointer ) Object( argument );
Quote:Original post by abdulla
You could use inplace new:
new( malloced_pointer ) Object( argument );


I am most confused by why you insist on using malloc for something it was never designed to do. However as abdulla said, placement new sounds vaguely like what you're looking for.
There's no real point in simply using malloc to allocate space for one item and directly following that with use of placement new. That's duplicating what 'new' usually does.
You would realistically only use "placement new" for things like memory pools, where you allocate space for many items, and then construct them on demand. I'm sure that's what you intend to do though.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Exactly! ^_^

Yeah, I realize the question was silly, but I was curious if there was a way.

Though I am doing something similar to my example with allocating a large block of memory and allocating out of it as I need... it has neat advantages that I like, such as if say I'm in a gameplay state and I allocate tons of things for it, and I need to exit out back to main menu... I don't really have to be "proper" about it and go track down all my allocated memory... I can just wipe the mempool clean, and exit back...

Plus I can write my own memory tracking, and I'm guaranteed how quickly allocation will run once I have my larger block allocated (work pounded into that I should never use new/delete while not in a loading phase).

Anyway, more backstory than anyone probably cared to hear... anyway!

Thanks so much!

Network EngineerVolition Inc.
I don't know if it is standards compliant, but using Microsofts compiler, you can do:

p_cb->CB::CB();
Quote:Original post by TtDTtW
*p_cb = CB() does indeed call the constructor, but doesn't set the vtable...


Keep in mind that what is happening here is that a temporary CB is created and then assigned to *p_cb. You aren't calling the constructor for *p_cb. If CB's assignment operator requires a properly constructed destination, then this will fail (not to mention it doesn't fill in the vtable entries, as you said).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Consider looking at your compiler's implementation for std::vector. It typically invokes this kind of magic.

This topic is closed to new replies.

Advertisement