Jump to content
  • Advertisement
Sign in to follow this  
uncutno

Memory manager and class constructors!

This topic is 5472 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

Hello! i want to create a realy simple memory manager. simply just a wrapper for new and delete with some ekstra stuff... (logging, garbage...) Now this is my problem: if i write: MyClass *class = new MyClass(); delete class; both the constructor and destructor of MyClass will be called... if i instead write: MyClass *class = MyNew(size); where MyNew only returns a pointer to some free mem i allocated at startup, the constructor will not be called... how could i solve this? (without advanced templates... an ekstra variable is ok...) -anders

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by uncutno
constructor will not be called... how could i solve this?
(without advanced templates... an ekstra variable is ok...)


you can use placement new to initialize uninitialized memory e.g.


#include <new>

/*....*/

MyClass *class = MyNew(size);

class = new(class) Myclass;


you need to include header new.

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Which should also be followed by an explicit call to its destructor

class->~MyClass();


and then deallocate the memory, if you wont to get picky about it [smile]

Share this post


Link to post
Share on other sites
thanks!!

Is there a way do do this inside MyNew and MyDelete? Calling the constructor without the class definition? (sound like it should be illegal)

can this be solved with a templated function in a class? it should be possible, but is this the right way to do it?

im thinking about a mem manager where all memory is allocated at start, and deleted at the end... will fitt perfectly with my sort of paging world data mem system...

Share this post


Link to post
Share on other sites
With templates, yes.

template<typename T>
T* MyNew(std::size_t size) {
//allocate
new (mem) T();
}



roughly the same idea with MyDelete.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!