If I were to have the code below, each module being independently compiled, would the allocation 'new char[10]' (start at 'ProcessFunction()') use the heap for Module 1 or Module 2? The in-place new call is made in Module 1, however the actual object is constructed on Module 2's heap. Also, does 'ProcessedObject' need to be delete[]d as an 'unsigned char*', or can I delete it as-is? Thanks.
ta0kira
Code:
//Module 1----------------------------------------------------------------------
class Object
{
public:
Object() : Data(new char[10]) {}
void
ObjectFunction();
~Object() { delete[] Data; }
private:
char
*Data;
};
size_t
ObjectSize() { return sizeof(Object); } //Give size of object to be created
Object
*InsertObject(Object *oObj) { return new(oObj) Object(); } //Create object
//------------------------------------------------------------------------------
//Module 2----------------------------------------------------------------------
class Object;
extern
size_t
ObjectSize();
extern
Object
*InsertObject(Object*);
void
ProcessFunction()
{
//Allocate space and insert a new object
Object *ProcessedObject =
InsertObject(reinterpret_cast <Object*> (new unsigned char[ObjectSize()]) );
//Call a function
ProcessedObject->ObjectFunction();
//Deallocate space
delete ProcessedObject;
//-or?-
ProcessedObject->~Object();
delete[] reinterpret_cast <unsigned char*> (ProcessedObject);
};
//------------------------------------------------------------------------------
//Main--------------------------------------------------------------------------
int main()
{
ProcessFunction();
return 0;
}
//------------------------------------------------------------------------------