Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?

Reply
Thread Tools

why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?

 
 
jimjim
Guest
Posts: n/a
 
      04-11-2004
Hello,

Consider the following:
class X{
public:
X(){ cout << "X constructor\n"; }
~X() { cout << "X destructor\n"; }
};

int main(){
X x;
list<X>lists;
lists.push_back(x);
lists.erase(lists.begin());
}

the output is:
X constructor //the constructor is called with the statement X x;
X destructor //the destructor is called with the statement
lists.erase(lists.begin()) (??)
X destructor

1. Above, why is the destructor called twice?
2. Does std::list::erase( ) delete both the x object and the Node object in
the list? Should I assume that the Node object in the list holds the object
as a whole as data, or just a pointer to it? Can you please answer with
reference to the implementation of std::list (near which line in the source
code should I look at)?

consider the following changes to main:
int main(){
X *x = new X();
list<X*>lists;
lists.push_back(x);

lists.erase(lists.begin());
}

the output is:
X constructor

3.Why doesn't (or seems that doesn't) lists.erase(lists.begin()) delete the
*x object ? ((I need to use delete (*i) to explicitly free the actual
object))
4. Should I assume that only the Node object in the list is deleted but not
the *x object? Why does this happen? Again, can you please expain with
reference to the implementation of std::list?

Thank you in advance for your help.



 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      04-11-2004
jimjim wrote in news:qFcec.1138$(E-Mail Removed) in
comp.lang.c++:

> Consider the following:
> class X{
> public:
> X(){ cout << "X constructor\n"; }
> ~X() { cout << "X destructor\n"; }


X( X const & ) { cout << "X copy constructor\n"; }

> };
>


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
jimjim
Guest
Posts: n/a
 
      04-11-2004
P.S: I am using g++ 2.95.2 on solaris (dunno how find the library that I
use in SunOS 5.


 
Reply With Quote
 
jimjim
Guest
Posts: n/a
 
      04-11-2004
can't get it! I have changed my class definition but after calling erase
nothing happens


 
Reply With Quote
 
jimjim
Guest
Posts: n/a
 
      04-11-2004
I would appreciate helping me understand why erase doesnt delete the object
when I store a pointer to the object.

Thanks in advance


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      04-11-2004
jimjim wrote in news:Y3dec.1164$(E-Mail Removed) in
comp.lang.c++:

> I would appreciate helping me understand why erase doesnt delete the
> object when I store a pointer to the object.
>
> Thanks in advance
>
>


#include <iostream>
#include <ostream>
#include <list>

using namespace std;

class X
{
public:
X(){ cout << "X constructor\n"; }
~X() { cout << "X destructor\n"; }
X( X const & ) { cout << "X copy constructor\n"; }

};

int main()
{
X x;
list<X>lists;
lists.push_back(x);
lists.erase(lists.begin());
}

output:

X constructor
X copy constructor
X destructor
X destructor


Note 2 constructors followed by 2 destructors:

The 1st ctor is for local 'x'.
The 2nd ctor is for the *copy* that is pushed into 'lists'.
The first dtor is for the X erased by erase( lists.begin() ).
The last dtor is for local 'x'.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      04-11-2004
On Sun, 11 Apr 2004 15:00:08 GMT, "jimjim" <(E-Mail Removed)>
wrote:

>I would appreciate helping me understand why erase doesnt delete the object
>when I store a pointer to the object.


Erasing an element from a container will apply a destructor to that
element, but raw pointers don't have destructors. If you want pointers to
automatically have a "delete" operation applied to them when you erase them
from an STL container, you have to use a "smart pointer" such as
boost::shared_ptr.
-leor

>Thanks in advance
>


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
jimjim
Guest
Posts: n/a
 
      04-11-2004
ha, I now got it

So when I push x into the list, I actually create a new copy of the x object
and store it in the list. This means that changing any state of the object
in the list wont affect the object I created with X x; right?


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      04-11-2004
jimjim wrote in news:ESdec.1189$(E-Mail Removed) in
comp.lang.c++:

> ha, I now got it
>
> So when I push x into the list, I actually create a new copy of the x
> object and store it in the list. This means that changing any state
> of the object in the list wont affect the object I created with X x;
> right?
>


Yup.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-11-2004
jimjim wrote:

> Hello,
>
> Consider the following:
> class X{
> public:
> X(){ cout << "X constructor\n"; }
> ~X() { cout << "X destructor\n"; }
> };
>
> int main(){
> X x;
> list<X>lists;
> lists.push_back(x);
> lists.erase(lists.begin());
> }
>


This code does not compile:

"C:\cygwin\bin\gcc.exe" -W -Wall -Wcast-align -Wwrite-strings
-Wno-sign-compare -Wno-unused-parameter -Wpointer-arith -Wundef
-Wcast-qual -ansi -pedantic-errors -ggdb -c fun.cpp
fun.cpp: In constructor `X::X()':
fun.cpp:3: error: `cout' undeclared (first use this function)
fun.cpp:3: error: (Each undeclared identifier is reported only once for
each
function it appears in.)
fun.cpp: In function `int main()':
fun.cpp:9: error: `list' undeclared (first use this function)
fun.cpp:9: error: syntax error before `>' token
fun.cpp:10: error: `lists' undeclared (first use this function)
Terminated with exit code 1

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
need help creating a two dimensional vector that holds pointers of auser defined type dwightarmyofchampions@hotmail.com C++ 9 04-08-2009 12:45 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Creating a class which holds a container with pointers to itself axel22@gmail.com C++ 4 08-07-2006 09:45 PM
WAS: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them? jimjim C++ 10 04-14-2004 07:04 PM



Advertisments