Stupidity again?

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

Recommended Posts

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 on other sites
You must include the standard new header in order to use placement new.

I was...

Share on other sites
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 on other sites
sorry but, what's wrong about allocating with malloc?

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

• 12
• 10
• 10
• 11
• 18