Oleg
Oleg

Reputation: 243

Valid constructor invoking in memory management

I have written own memory library which helps me to avoid memory leaks and to avoid fragmentation problems. All works fine. The main problem is it doesn't work valid with classes. When i call my_alloc(size) i want to automatically call constructor if it exists. Can i do it without overloading new operator?

Upvotes: 0

Views: 172

Answers (2)

Zan Lynx
Zan Lynx

Reputation: 54325

Check your C++ implementation. Some of them (I think the G++ compiler does this) call the C Runtime malloc to get the memory for new, then call the constructors.

If you have one of those implementations, all that you need to do is properly override the standard library malloc and free functions (read the library internals documentation) and C++ will work automatically.

Upvotes: 1

littleadv
littleadv

Reputation: 20272

You can use placement new on your allocated memory, to invoke the constructor without letting new do the allocations.

What's wrong with overloading new?

Upvotes: 2

Related Questions