Memory management - virtual function table

Started by
2 comments, last by SiCrane 19 years, 6 months ago
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
Advertisement
Use placement new:

class C
{
};

void* data = malloc(sizeof C);
C* c = new(data) C(); // run ctor, should fill out VFT as well
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Thanx, it worked fine!
Does it have any impact over memory usage?
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();

This topic is closed to new replies.

Advertisement