Is it posible to set a chunk of memory to an object?

Started by
15 comments, last by Kwizatz 19 years, 10 months ago
Ok, here is the dilema: lets say I have class:

class CSomeClass
{
public:
CSomeClass(){};
CSomeClass(){};
void SomeFunctionThatManipulatesSomeString(){ SomeString = "Hello";};
private:
std::string SomeString;
};
 
And then I have a chunk of memory which is sizeof(CSomeClass), the Memory is allocated for me by some API, so I have no control over the process and I MUST use that chunk of memory, how do I set that chunk of memory to be a CSomeClass object? The following works to some degree but. . .

// the following line is for demonstration purposes, so
// don''t ask me why I am not using new, this represents the
// part I have no control over, and I think it pretty much helps
// set the scenario
void* memorychunk = malloc(sizeof(CSomeClass));

// and then
((CSomeClass*)memorychunk)->SomeFunctionThatManipulatesSomeString();
 
... Constructors/Destructors are never called, so string::string is never called for SomeString, and the application crashes right at the assignment, now the following I thought would work:

// the following line is for demonstration purposes, so
// don''t ask me why I am not using new, this represents the
// part I have no control over, and I think it pretty much helps
// set the scenario
void* memorychunk = malloc(sizeof(CSomeClass));

// and then
CSomeClass* pSomeClass = ((CSomeClass*)memorychunk);
*pSomeClass = CSomeClass();
pSomeClass->SomeFunctionThatManipulatesSomeString();
 
But this one crashes right after the Constructor call, any ideas? Thanks in advance Aeon Games
Advertisement
To allocate a new CSomeClass in memory chunk:

CSomeClass * ptr = new (memorychunk) CSomeClass();

To delete the object later:

ptr->~CSomeClass();
Excelent!, I used
new (memorychunk) CSomeClass();

And it worked as I needed it too, Thanks SiCrane! :D

EDIT: if I have an empty destructor, do I still need to call it the way you pointed out?
I dont like having a "new" call without matching "delete", but I guess if I call delete on the pointer it would crash later when "free" tries to free the already deleted memory.

Aeon Games

[edited by - Kwizatz on June 1, 2004 11:05:52 PM]
My guess to the crashing (I could be wrong - someone please correct me if I am) is that your constructor probably crashes the program because the pointer pSomeClass is pointing to memory, which effectively bypasses the constructor's purpose (which is to allocate memory and make initializations).

Something seems very strange about the API you mentioned. How can an API allocate memory for you before you need to use it, then tell you to go find it? Or am I mistaken about how it works?

Stick with new and delete for C++ classes. malloc() is only legacy of C code, and while it can still be used to allocate objects it's not proper in C++.

EDIT: beat by a few minutes... even though I'm still horribly mistaken with my answer
_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.


[edited by - MajorShredd on June 1, 2004 11:16:04 PM]
@MajorShredd

Well, its the tsxapi, the plugin api for trueSpace, internally it seems to be C++, however (and think this is good because of C++ name mangling) the interfaces are C, there is a group of functions that allow you to easily asign external, custom data to objects (Polyhedrons,lights,etc) and the way it works is that you register the data type and its size (with sizeof()), register some callbacks for dealing with the data, and then you call a CreateData() function that allocates memory and returns a pointer to said memory, you can then access the chunk by calling a GetData() function.

All that was pretty irrelevant to the question so I omited it, but since you showed some interest, there it is

Aeon Games
Thanks for the clarification, Kwizatz
quote:Original post by Kwizatz
EDIT: if I have an empty destructor, do I still need to call it the way you pointed out?

Yes, you should. The reason is that you have an member variable with a non-trivial destructor in your class. If you don''t call the class''s destructor, the string''s destructor won''t fire and you''ll probably leak memory.

quote:
I dont like having a "new" call without matching "delete", but I guess if I call delete on the pointer it would crash later when "free" tries to free the already deleted memory.

Yes, well, placement new is a funny beast. If you try thinking of it as an explicit constructor call instead of a funny looking new, it might help things.
Alright thanks, I did incorporate the call to the destructor, no big deal really and it works as expected, just looks funny as you said yourself.

Aeon Games
quote:Original post by Kwizatz
Excelent!, I used
new (memorychunk) CSomeClass();

You should really be using this line instead:
new (memorychunk) CSomeClass;


- Houdini
- Houdini
quote:Original post by Houdini
quote:Original post by Kwizatz
Excelent!, I used
new (memorychunk) CSomeClass();

You should really be using this line instead:
new (memorychunk) CSomeClass;


- Houdini


It really doesn''t matter. The potential problem here is that the memory returned by the allocator might not be properly aligned so it''s not portable, but it''s most likely a non-issue on windows anyway.

This topic is closed to new replies.

Advertisement