🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #7067

Re: memory control over vector

#7067
From: Thomas Maeder [TeamB]
Date: Sat, 19 Mar 2005 16:37
48 lines
2498 bytes
"Boguslaw Fries" <boguslaw.fries@polivision.it> writes:

> I extensively use STL library. I use many times the following
> construction: stack< Myobj *> mystack;
>
> Myobj  * pMyobj;
>
>  if( .... )
> {
>     pMyobj = new   Myobj  ;
>     mystack.push( pMyobj );
> }
> -....
>
> and now:
> mystac.pop( ); <-have I delete pMyobj ?

Yes and no. You have to make sure that the delete expression is
evaluated. You don't necessarily have to evaluate it "yourself". See
below.


> If yes, how to nicely delete objects in the program when I have many
> returns?

Change the element type of mystack:

typedef std::stack< boost::shared_ptr<Myobj> > mystack_type;
mystack_type mystack;
if (...)
  mystack.push(mystack_type::value_type(new Myobj));

Apart from that, no changes in your code should be necessary.


Classes generated from the class template boost::shared_ptr have the
properties required for being an element of a Standard C++ Library
container template (which are Assignable and
CopyConstructible).

Thanks to appropriate operator overloading (*, ->, =), objects of such
classes behave much like "raw" pointers. But the destructor causes the
object that the pointer refers to to be deleted when it's no longer
referenced by any pointer.


See http://www.boost.org/ - you'll find a lot of useful stuff for C++
programmers.
Technical Details
Message-ID: <m2d5tviocq.fsf@madbox2.local>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!63.218.45.11.MISMATCH!newshosting.com!nx02.iad01.newshosting.com!novia!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <423c38d8@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,62ed433aa0de877f
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: memory control over vector
References: <423c38d8@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Sat, 19 Mar 2005 17:37:09 +0100
Message-ID: <m2d5tviocq.fsf@madbox2.local>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:1iznfhqgA5mpx3mxAAEqDln99dE=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 217.162.1.33
X-Trace: newsgroups.borland.com 1111250231 217.162.1.33 (19 Mar 2005 08:37:11 -0800)
Lines: 48
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!63.218.45.11.MISMATCH!newshosting.com!nx02.iad01.newshosting.com!novia!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail