# Stupidly retarded...

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

But neccessary. And I'd prefer not to get talked out of this one, because I don't really have an option. And "ditching" the design to make abstract-C++-ness happy can't be a solution. How do I call constructors/destructors on objects that have been allocated their memory by malloc()?

##### Share on other sites
Placement new

It's hard to imagine why a design(and not an implementation necessity) would require an object to be placed at specific memory locations, but whatever. Placement new is what you need.

##### Share on other sites
Quote:
 Original post by dbzprogrammerAnd I'd prefer not to get talked out of this one, because I don't really have an option. And "ditching" the design to make abstract-C++-ness happy can't be a solution.

I'm very interested in at least hearing why this is.

##### Share on other sites
I'm not saying that it is always so, or that it is so in your case. But usually when you need help you are not qualified to determine whether there are alternative solutions or not. You do as you wish though.

##### Share on other sites
Sorry guys, it was late last night and I was trying to figure stuff out. This is going to take a while to explain, so I didn't want to do it then.

In my game engine, I have the GameEngine namespace. In the engine, there is a global header that is #included by all files except a few base files. These include the Kernel, MemoryManager, Log, and EventManager classes because they are included into the Global.h.

In Global.h I have a section that goes somewhere along the lines of:

//Outside the GameEngine namespace#ifdef DEBUG#define DEBUG_NEW new(__LINE__,__FILE__)#define DEBUG_DELETE delete(__LINE__,__FILE__)#else#define DEBUG_NEW new#define DEBUG_DELETE delete#endif#define new DEBUG_NEW#define delete DEBUG_DELETE//Inside the GameEngine namespace#ifdef DEBUGvoid* operator new(size_t,int,char*);void* operator new[](size_t,int,char*);void operator delete(void*,int,char*);void operator delete[](void*,int,char*);#elsevoid* operator new(size_t);void* operator new[](size_t);void operator delete(void*);void operator delete[](void*);#endif

Later, in the MemoryManager.cpp, I have all these function defined.

Which brings me to my delema. I think that even though I have these functions declared in the Global.h, that some of the .cpp files cannot find the definition of these functions.

More importantly, my current problem. All my classes are in the GameEngine namespace, so naturally in order to use the normal new allocater in the global namespace, I need to call ::new and ::delete.

Which works for Kernel and MemoryManager, but not for Heap. Heap is a class that holds several MB of data in it for future allocations. When I call Heap* newHeap = ::new Heap; //inside the namespace, I get a stack corruption error and somehow the class allocated itself into 0x1c. Definately not right.

When I malloc() it though, I have no problems. However, the constructor doesn't get called, and that doesn't help me find the error if it's inside the Heap class. I know that there's a couple syntax flaws (such as having the DEBUG_NEW new affecting all new commands, in and out of the namespace), but that shouldn't be affecting what's happening.

One small thought I had while typing this up (I can't access my code right now), is if I had #define DEBUG_NEW_M new[] for new[], then make other defines for the delete[] and stuff.

Anyways, I'd appreciate any and all thoughts guys.