STL list problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • giotheninman@gmail.com

    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 >));
    SetupListWithou tGettingNewMemo ry(myObj->std::list<tObj >);
    myObj->myList->insert(somethi ng);

  • Victor Bazarov

    #2
    Re: STL list problems

    giotheninman@gm ail.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 >));
    SetupListWithou tGettingNewMemo ry(myObj->std::list<tObj >);
    myObj->myList->insert(somethi ng);
    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(s td::list<tObj>) );
    myObj->myList = new (buffer) std::list<tObj> ;
    myObj->myList->insert(somethi ng);

    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


    Comment

    • Bernd Strieder

      #3
      Re: STL list problems

      Hello,

      Victor Bazarov wrote:
      giotheninman@gm ail.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

      Comment

      Working...