Memory management - virtual function table
Im not using 'new' to allocate memory in my app, only casting a chunk of memory from a big pool pre-alocated....it works for classes that do not inherit from another ones...
How do i fill the VFT after alocating a "generic" memory to a variable? I read somewhere to do something like
cCLASS_A *a = (cCLASS_A*)my_mem_sys.Alocate(sizeof(cCLASS_a));
a = new (cCLASS_A*); //something like this, i really dont know
Im trying to do not allocate any memory in conventional way, to avoid fragmentation...and about using the method above to fill the VFT, does it allocate any memory? Thanks
Use placement new:
class C
{
};
void* data = malloc(sizeof C);
C* c = new(data) C(); // run ctor, should fill out VFT as well
class C
{
};
void* data = malloc(sizeof C);
C* c = new(data) C(); // run ctor, should fill out VFT as well
It won't allocate any memory from the heap, if that's what you're asking. However, it does call the class's constructor, so if the constructor calls new/malloc()/whatever, then additional memory will be allocated.
Just remember to call the destructor when you're done with it. Using antareus' example, the syntax would look like:
c->~C();
Just remember to call the destructor when you're done with it. Using antareus' example, the syntax would look like:
c->~C();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement