Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL abuse (http://www.velocityreviews.com/forums/t280473-stl-abuse.html)

Graeme 01-07-2004 11:42 AM

STL abuse
 
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 01-07-2004 12:02 PM

Re: STL abuse
 
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" <g.winter@dl.ac.uk> wrote in message
> news:bth0n7$m9h$1@mserv2.dl.ac.uk...
>> 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 01-07-2004 01:22 PM

Re: STL abuse
 
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" <g.winter@dl.ac.uk> wrote in message
news:bth0n7$m9h$1@mserv2.dl.ac.uk...
> 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




Rolf Magnus 01-07-2004 02:25 PM

Re: STL abuse
 
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 01-07-2004 02:37 PM

Re: STL abuse
 
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;



tom_usenet 01-07-2004 05:10 PM

Re: STL abuse
 
On Wed, 07 Jan 2004 11:42:35 +0000, Graeme <g.winter@dl.ac.uk> 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


All times are GMT. The time now is 10:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.