469,601 Members | 2,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

STL list problems

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);

Jul 31 '06 #1
2 1541
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
Jul 31 '06 #2
Hello,

Victor Bazarov wrote:
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.
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...
But overloading new and delete for tObj won't help anything here. The
tObj instances are usually put into objects of a class used for the
nodes, which will be allocated by the allocator object given by default
or by the user to the list object at construction time. All you can do
is learning to use allocators.

There is no sensible reason to use malloc in this way. If you write why
you cannot live with the defaults, perhaps you can get better advice.

Bernd Strieder

Aug 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Moosebumps | last post: by
5 posts views Thread by Max Bentz | last post: by
14 posts views Thread by Dave | last post: by
9 posts views Thread by ogerchikov | last post: by
8 posts views Thread by dmp | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.