STL abuse

Discussion in 'C++' started by Graeme, Jan 7, 2004.

  1. Graeme

    Graeme Guest

    Hi All,

    I am trying to use the standard template library to code an extension to
    Python. However, at the core of the Python object mechanism the allocator
    uses malloc() rather than new, so I need a way to manually initialise an
    STL list - is there a way to do this?

    I need to do something like:

    obj = (obj *) malloc (sizeof(obj));
    doSomethingMagic(obj->myList);
    obj->myList.push_back(foo);

    Cheers,

    Graeme
     
    Graeme, Jan 7, 2004
    #1
    1. Advertising

  2. Graeme

    Graeme Guest

    Hi Catalin,

    The problem here is that I have no control over the allocator - the
    allocation is being performed somewhere in the background by Python, using
    the PyObject_New function, which has at it's heart malloc (I think - it
    uses sizeof(Class) somewhere.) If I had control over the allocation I don't
    think that I would be in this mess.

    Cheers,

    Graeme

    Catalin Pitis wrote:

    > Hi
    >
    > You should define your own allocator class. The STL list has the template
    > parameter A having implicit value as std::allocator< E>.
    >
    > Catalin
    >
    >
    > "Graeme" <> wrote in message
    > news:bth0n7$m9h$...
    >> Hi All,
    >>
    >> I am trying to use the standard template library to code an extension to
    >> Python. However, at the core of the Python object mechanism the allocator
    >> uses malloc() rather than new, so I need a way to manually initialise an
    >> STL list - is there a way to do this?
    >>
    >> I need to do something like:
    >>
    >> obj = (obj *) malloc (sizeof(obj));
    >> doSomethingMagic(obj->myList);
    >> obj->myList.push_back(foo);
    >>
    >> Cheers,
    >>
    >> Graeme
     
    Graeme, Jan 7, 2004
    #2
    1. Advertising

  3. Hi

    You should define your own allocator class. The STL list has the template
    parameter A having implicit value as std::allocator< E>.

    Catalin


    "Graeme" <> wrote in message
    news:bth0n7$m9h$...
    > Hi All,
    >
    > I am trying to use the standard template library to code an extension to
    > Python. However, at the core of the Python object mechanism the allocator
    > uses malloc() rather than new, so I need a way to manually initialise an
    > STL list - is there a way to do this?
    >
    > I need to do something like:
    >
    > obj = (obj *) malloc (sizeof(obj));
    > doSomethingMagic(obj->myList);
    > obj->myList.push_back(foo);
    >
    > Cheers,
    >
    > Graeme
     
    Catalin Pitis, Jan 7, 2004
    #3
  4. Graeme

    Rolf Magnus Guest

    Graeme wrote:

    > Hi All,
    >
    > I am trying to use the standard template library to code an extension
    > to Python. However, at the core of the Python object mechanism the
    > allocator uses malloc() rather than new, so I need a way to manually
    > initialise an STL list - is there a way to do this?
    >
    > I need to do something like:
    >
    > obj = (obj *) malloc (sizeof(obj));
    > doSomethingMagic(obj->myList);
    > obj->myList.push_back(foo);


    Let's see if I understood. Your obj class has a std::list as member, and
    since obj is created using malloc, the list is not properly contructed?
    You can do that with placement new from the header <new> by replacing
    the doSomethingMagic call with:

    new(obj->myList) std::list;
     
    Rolf Magnus, Jan 7, 2004
    #4
  5. Graeme

    Graeme Guest

    Hi Rolf,

    Just the ticket, thankyou.

    Graeme


    Rolf Magnus wrote:

    > Graeme wrote:
    >
    >> Hi All,
    >>
    >> I am trying to use the standard template library to code an extension
    >> to Python. However, at the core of the Python object mechanism the
    >> allocator uses malloc() rather than new, so I need a way to manually
    >> initialise an STL list - is there a way to do this?
    >>
    >> I need to do something like:
    >>
    >> obj = (obj *) malloc (sizeof(obj));
    >> doSomethingMagic(obj->myList);
    >> obj->myList.push_back(foo);

    >
    > Let's see if I understood. Your obj class has a std::list as member, and
    > since obj is created using malloc, the list is not properly contructed?
    > You can do that with placement new from the header <new> by replacing
    > the doSomethingMagic call with:
    >
    > new(obj->myList) std::list;
     
    Graeme, Jan 7, 2004
    #5
  6. Graeme

    tom_usenet Guest

    On Wed, 07 Jan 2004 11:42:35 +0000, Graeme <> wrote:

    >Hi All,
    >
    >I am trying to use the standard template library to code an extension to
    >Python. However, at the core of the Python object mechanism the allocator
    >uses malloc() rather than new, so I need a way to manually initialise an
    >STL list - is there a way to do this?
    >
    >I need to do something like:
    >
    >obj = (obj *) malloc (sizeof(obj));
    >doSomethingMagic(obj->myList);
    >obj->myList.push_back(foo);


    Rather, you may as well initialize the whole object using placement
    new. e.g.

    new (obj) ObjectType;

    That will construct any members that need constructing, including
    lists. Remeber to destruct using:

    obj->~ObjectType();

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Jan 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rossz
    Replies:
    31
    Views:
    1,066
    Rincewind
    Sep 25, 2005
  2. Richard

    Complaints to

    Richard, Jul 22, 2004, in forum: C++
    Replies:
    0
    Views:
    1,802
    Richard
    Jul 22, 2004
  3. Kal Iyer
    Replies:
    10
    Views:
    654
    Victor Bazarov
    Dec 9, 2004
  4. Paul
    Replies:
    1
    Views:
    351
    bruce barker \(sqlwork.com\)
    Jun 29, 2006
  5. Alexander Schmolck

    is there a simple way to abuse parser for this?

    Alexander Schmolck, Oct 1, 2004, in forum: Python
    Replies:
    0
    Views:
    254
    Alexander Schmolck
    Oct 1, 2004
Loading...

Share This Page