Jump to content
  • Advertisement
Sign in to follow this  
xtrmntr

alternative to placement new?

This topic is 5408 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

First I will explain what I'm trying to achieve and then what my restrictions are. I have a class in my library and I wish to place it at a specific spot in memory (which my program owns). The restrictions are that I can't using any standard libraries, nor can I allocate my own memory via new/malloc. The person using my library would first make an API call which will tell them how much memory my library will need to run. The integrator will then allocate this piece of memory from the system they are using my library on and will pass this chunk to me. Inside of one of these API calls I want to create a class (derived one in this case). Currently to get around not using new I'm doing the following (in its simplistic form):

#define MAX_INSTANCE_SIZE sizeof(MyClass)

typedef struct
{
   unsigned char memory[MAX_INSTANCE_SIZE];
   unsigned int size;
} HeapBlock;

HeapBlock forMyClass;

void GetClass(Base * * nppClass)
{
   MyClass p;
   My_memcpy(forMyClass.memory, &p, sizeof(MyClass));
}

This works just fine but I'm interested in other alternatives to this. Any help would be greatly appreciated! (p.s. yes, I understand that the above will result in the constructor not being called)

Share this post


Link to post
Share on other sites
Advertisement
If the memory is being allocated for you, then the client already knows how to access the class (because it can cast the pointer just as well as you can). So essentially all you need to do is fire the constructor, which is exactly what placement new (with a single void* argument) does. You can encapsulate that in a function if you like, but really it's the right way to go.

Share this post


Link to post
Share on other sites
Sneftl,

So you are saying I can define the following function in my class without any needed headers?

class MyClass
{
public:
static void* operator new(size_t s, void* mem)
{
return mem;
}
}


then inside my library function call do

MyClass * p = new(heapBlock.memory) MyClass();

?


edit:

The reason I ask this is that while looking for placement new stuff on the forum I came across a post saying that this type of placement new did not work on GCC unless you include the <new> header file.

This is one of my restrictions.

Share this post


Link to post
Share on other sites
Yep, it'll work fine. In fact, that's the definition of placement-new(void*) right there (except that you've defined it at class level, which of course is what you want).

Share this post


Link to post
Share on other sites
Awesome!

I guess I was just not using it correctly when I tried switching to this safer way of doing it.

Suppose I have been lucky that the memcpy way worked on the variety of compilers that I built my library for (ARM, MCORE, Infineon, etc).

Thanks :)

Share this post


Link to post
Share on other sites
The memcpy() you were doing can only be guarentteed to work if the class only contains built-in non-pointer variables, no virtual functions and it's default constructor doesn't rely on anything external to the class (eg a global counter that gets incremented on every construction).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!