Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL abuse

Reply
Thread Tools

STL abuse

 
 
Graeme
Guest
Posts: n/a
 
      01-07-2004
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
 
Reply With Quote
 
 
 
 
Graeme
Guest
Posts: n/a
 
      01-07-2004
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" <(E-Mail Removed)> wrote in message
> news:bth0n7$m9h$(E-Mail Removed)...
>> 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


 
Reply With Quote
 
 
 
 
Catalin Pitis
Guest
Posts: n/a
 
      01-07-2004
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" <(E-Mail Removed)> wrote in message
news:bth0n7$m9h$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-07-2004
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;

 
Reply With Quote
 
Graeme
Guest
Posts: n/a
 
      01-07-2004
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;


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      01-07-2004
On Wed, 07 Jan 2004 11:42:35 +0000, Graeme <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abuse of the Net/Abuse on the Net Dr Wankfest Computer Support 14 07-19-2006 10:31 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
Fighting abuse with abuse Mara Computer Support 70 03-24-2005 08:30 PM
Re: Fighting abuse with abuse Peter =?UTF-8?B?S8O2aGxtYW5u?= Computer Information 0 03-22-2005 10:31 AM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments