Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ownership of memory in stl containers??

Reply
Thread Tools

ownership of memory in stl containers??

 
 
Khalid
Guest
Posts: n/a
 
      03-02-2004
II am allocating alot of memory for my problem model which uses stl containers
for these pointers,
will stl free the memory?

by other words what is the semantics of memory ownership in stl?

thanks a lot

kh
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-02-2004

"Khalid" <(E-Mail Removed)> wrote in message
news:c22nvb$(E-Mail Removed)...
> II am allocating alot of memory for my problem model which uses stl

containers
> for these pointers,
> will stl free the memory?


No.

>
> by other words what is the semantics of memory ownership in stl?


You allocated it, you free it.

STL always operates on a copy of whatever you give to it. However a copy of
a pointer is just a pointer.

It actually much better to avoid raw pointers and the STL. Maybe you should
conisder using a smart pointer instead?

>
> thanks a lot
>
> kh


john


 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      03-02-2004
STL will free the memory *it* allocates to store your pointers. However,
you are responsible for deallocating the memory your pointers point to. STL
can't possibly know to free that memory because, for all STL knows, your
pointers could be pointing to memory that was not dynamically allocated.

If you want the deallocation of your memory to happen automatically, you
might consider using a smart pointer rather than a pointer. STL *will* call
the destructor of the elements it holds if those elements are of class type.
In the case of a smart pointer, that destructor call would result in the
deallocation of your memory (though, again, STL is not doing it directly).

HTH,
Dave


"Khalid" <(E-Mail Removed)> wrote in message
news:c22nvb$(E-Mail Removed)...
> II am allocating alot of memory for my problem model which uses stl

containers
> for these pointers,
> will stl free the memory?
>
> by other words what is the semantics of memory ownership in stl?
>
> thanks a lot
>
> kh



 
Reply With Quote
 
Thorsten Ottosen
Guest
Posts: n/a
 
      03-03-2004


> "Khalid" <(E-Mail Removed)> wrote in message


> It actually much better to avoid raw pointers and the STL. Maybe you

should
> conisder using a smart pointer instead?


yes, for now you should use boost::shared_ptr.

Alternatively you might look at my library (which is in the review queue at
boost) at
http://cvs.sourceforge.net/viewcvs.p...ptr_container/
http://cvs.sourceforge.net/viewcvs.p...container/doc/

br

Thorsten


 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      03-03-2004
John Harrison wrote:

> STL always operates on a copy of whatever you give to it. However a copy of
> a pointer is just a pointer.
>
> It actually much better to avoid raw pointers and the STL. Maybe you should
> conisder using a smart pointer instead?


I've seen this suggested many times, but I've never read enough to
understand the benefits. My impression is that a "shared pointer" is an
object which encapsulates a pointer to the "real data." A copy of this
object is stored in the STL container. Since the container holds an
object (versus a pointer), the object destructor is called when the
container element is erase()d, and hence the real object is delete'd.
This is superior to storing raw pointers for which the caller must a)
hold a reference and coordinate object cleanup, or b) leak memory. Is
this at all accurate?

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-03-2004

"David Rubin" <(E-Mail Removed)> wrote in message
news:VF91c.756$(E-Mail Removed)...
> John Harrison wrote:
>
> > STL always operates on a copy of whatever you give to it. However a copy

of
> > a pointer is just a pointer.
> >
> > It actually much better to avoid raw pointers and the STL. Maybe you

should
> > conisder using a smart pointer instead?

>
> I've seen this suggested many times, but I've never read enough to
> understand the benefits. My impression is that a "shared pointer" is an
> object which encapsulates a pointer to the "real data." A copy of this
> object is stored in the STL container. Since the container holds an
> object (versus a pointer), the object destructor is called when the
> container element is erase()d, and hence the real object is delete'd.


Different smart pointers have different benefits. But for this situation
reference counting would be used which means that the real object would only
be deleted when the destructor for the last copy of the smart pointer is
invoked. This has many benefits whether or not those smart pointers happen
to be in STL container. Boost's shared pointer works like this (I guess
that's what makes it a _shared_ pointer).

> This is superior to storing raw pointers for which the caller must a)
> hold a reference and coordinate object cleanup, or b) leak memory. Is
> this at all accurate?


Not quite sure what you mean by hold a reference, but the rest seems
accurate enough. For me the big problem in holding raw pointer in STL
containers is that you can manage it easily enough if you have only a single
copy of the container but a soon as you start to want to make copies of the
container things rapidly get too complex and messy.

john


 
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
Implementing an STL memory pool with constant memory locations mojumbo C++ 2 10-16-2009 02:31 PM
Tomcat file ownership Mike Java 0 02-28-2006 09:39 PM
Object ownership and memory management jakub.pieczonka@gmail.com C++ 3 09-27-2005 02:49 PM
SQL Session State, Cross DB ownership Brian ASP .Net 0 05-03-2005 10:40 PM
Re: Printer -total cost of ownership EricP Computer Support 0 10-27-2003 02:32 PM



Advertisments