#### Archived

This topic is now archived and is closed to further replies.

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

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

## Recommended Posts

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

##### Share on other sites
To allocate a new CSomeClass in memory chunk:

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

To delete the object later:

ptr->~CSomeClass();

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

[edited by - Kwizatz on June 1, 2004 11:05:52 PM]

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

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

##### Share on other sites
Thanks for the clarification, Kwizatz

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

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

##### Share on other sites
quote:
Original post by Kwizatz
Excelent!, I used
new (memorychunk) CSomeClass();

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

- Houdini

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

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011470
• ### Who's Online (See full list)

There are no registered users currently online

×