Sign in to follow this  
dbzprogrammer

Stupidity again?

Recommended Posts

dbzprogrammer    100
Looking at the code below:
namespace GameEngine
{
    Kernel::Kernel()
    {
        memman = (MemoryManager*)malloc(sizeof(MemoryManager));
        memman = new(memman) MemoryManager();
        //or
        memman = ::new(memman) MemoryManager();
    }
}


I'm getting an error saying that it cannot find this new operator. This is the placement new, and it can't find it. What's going on?

Share this post


Link to post
Share on other sites
Deyja    920
Wait, had you overloaded an operator new for MemoryManager? Once you overload one, it hides the other forms.

I also find it quite ridiculous that you are using malloc to allocate space for a memory manager object. For that matter, you should prefer new to malloc anyway.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6930
There's nothing wrong, per se. But it's not consider appropriate, necessary or proper.
Two reasons:
1) Rule of thumb is to not use C idioms or C standard functions in C++ code.
2) The new operator is the C++ way to allocate memory. Also, it invokes the constructor of an object unlike malloc.

Share this post


Link to post
Share on other sites
dbzprogrammer    100
Yea, it is ridiculous!

Rebel!


namespace GameEngine
{
inline void* operator new(size_t tSize)
{ return (MemoryManager::get().Allocate(tSize); }
}





See the loop yet?

We found the problem could be solved my solution or overloading new/delete inside the class. For what we were doing, we didn't want to overload new/delete anymore.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this