开发者

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?


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜