0
101 Dec 19, 2004 at 13:43

I have a custome memory manager that overrides the new and delete operator in a pretty basic way:

#ifdef _DEBUG
#define MARY_DEBUG_NEW      new(__FILE__, __LINE__)
#else
#define MARY_DEBUG_NEW      new
#endif
#define new                 MARY_DEBUG_NEW
#endif


Now this works fine for almost evrything and anything except in one small area. I have started to work with AngelScript, and to use custom classes with the script (including strings) i need to use new in a way I have never seen before.

static void ConstructString(CScriptString* _thisPointer)
{
new(_thisPointer) CScriptString;
}


Now I need to alter my memory manager so that it can handle this syntax, but I have no idea how to do this, as I have _no_ idea what it does. According to the author of AngelScript, the _thisPointer has already been allocated, so i am assuming it just calls the constructor, and sets up the object.

Is this correct? IF it is, does anyone know how to implment this in my memory manager?

Spree

#### 2 Replies

0
154 Dec 19, 2004 at 18:01

this is called “placement new” which is used to construct an object on a pre-allocated buffer. Example:

    char *buffer = new char[50];            // pre-allocated buffer
string *ptr = new (buffer) string("hello world!"); // contruct string in buffer


To overload it, you need to redefine the following interface:

void operator new(size_t, void *where)
{
return (where);
}


Hope that helps.

0
101 Dec 19, 2004 at 21:06

Thanks a lot for that. I have found a way around using the new operator (which i prefer), meaning after 2 days of trying none stop it finally works!

Thanks for the info though, surprising what you dont know sometimes

Spree