Jump to content
  • Advertisement

Archived

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

Kwizatz

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

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

If you intended to correct an error in the post then please contact us.

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 Aeon Games

Share this post


Link to post
Share on other sites
Advertisement
To allocate a new CSomeClass in memory chunk:

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

To delete the object later:

ptr->~CSomeClass();

Share this post


Link to post
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.

Aeon Games

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

Share this post


Link to post
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 this post


Link to post
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

Aeon Games

Share this post


Link to post
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 this post


Link to post
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.

Aeon Games

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!