Jump to content
  • Advertisement
Sign in to follow this  
TtDTtW

Silly Constructor Question.

This topic is 3998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Consider looking at your compiler's implementation for std::vector. It typically invokes this kind of magic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!