STL abuse

G

Graeme

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
 
G

Graeme

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
 
C

Catalin Pitis

Hi

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

Catalin
 
R

Rolf Magnus

Graeme said:
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;
 
G

Graeme

Hi Rolf,

Just the ticket, thankyou.

Graeme


Rolf said:
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;
 
T

tom_usenet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top