gi**********@gmail.com wrote:
I have a stl list and i have a problem allocating memory for it. I am
using the malloc operator in order to get memory for it and I have
tried using placement new but it still isnt setting up the list
correctly.
The solution im looking for is something as follows
myObj->std::list<tObj= (std::list<tObj>
*)malloc(sizeof(std::list<tObj>));
SetupListWithoutGettingNewMemory(myObj->std::list<tObj>);
myObj->myList->insert(something);
What you're looking for is probably the "placement new" operator. You
will need to include <memoryheader and use something like
void* buffer = malloc(sizeof(std::list<tObj>));
myObj->myList = new (buffer) std::list<tObj>;
myObj->myList->insert(something);
The funny thing, of course, is that while you will be allocating the
memory for the list itself, you're still allowing the list to allocate
memory for the objects it creates. In order to take control over that,
you need to learn to use "allocators". You can also overload the 'new'
and 'delete' for your class ('tObj') and those things will be called
when another object of 'tObj' type is about to be created...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask