C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Segmentation fault in list...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
saratoga
Guest





PostPosted: Mon Jul 25, 2005 10:05 pm    Post subject: Segmentation fault in list... Reply with quote



Hi, all!
I've 'Segmentation fault' in this code. I've found that there are two
calls to destructor.

TestSTL.h:
#include <list>
#include <string>
#include <iostream>

using namespace std;

class TestSTL
{
string *_str;

public:
TestSTL() { _str = new string("test"); };
~TestSTL() { cerr << "destruct" << endl; delete _str; }
};


TestSTL.cpp:
#include "TestSTL.h"

int main()
{
list lst.push_back(TestSTL());
}

Could U explain to me what's wrong here?

Thanx.
With the best regards
Daniel.


Back to top
Thomas Tutone
Guest





PostPosted: Mon Jul 25, 2005 10:32 pm    Post subject: Re: Segmentation fault in list... Reply with quote



saratoga wrote:
Quote:
Hi, all!
I've 'Segmentation fault' in this code. I've found that there are two
calls to destructor.

TestSTL.h:
#include <list
#include #include
using namespace std;

class TestSTL
{
string *_str;

public:
TestSTL() { _str = new string("test"); };
~TestSTL() { cerr << "destruct" << endl; delete _str; }
};


TestSTL.cpp:
#include "TestSTL.h"

int main()
{
list lst.push_back(TestSTL());
}

Could U explain to me what's wrong here?

You failed to define an appropriate copy constructor. The
compiler-generated copy constructor is generally not appropriate for
classes (like yours) that acquire resources (in this case, by using
new).

Best regards,

Tom


Back to top
Ron Natalie
Guest





PostPosted: Mon Jul 25, 2005 11:57 pm    Post subject: Re: Segmentation fault in list... Reply with quote



Thomas Tutone wrote:

Quote:

You failed to define an appropriate copy constructor. The
compiler-generated copy constructor is generally not appropriate for
classes (like yours) that acquire resources (in this case, by using
new).

And an copy-assignment operator. Std containers require both.


Back to top
Ram
Guest





PostPosted: Tue Jul 26, 2005 6:13 am    Post subject: Re: Segmentation fault in list... Reply with quote

A side comment- Its not a good idea to say something like new string
(unless u are allocating an array of strings). Using std::string can
relieve u of having to take care of memory management.

Best regards
Ram

Back to top
Csaba
Guest





PostPosted: Wed Jul 27, 2005 2:04 pm    Post subject: Re: Segmentation fault in list... Reply with quote

"Thomas Tutone" <Thomas8675309 (AT) yahoo (DOT) com> wrote in
news:1122330726.406961.130430 (AT) g44g2000cwa (DOT) googlegroups.com:

Quote:
saratoga wrote:
Hi, all!
I've 'Segmentation fault' in this code. I've found that there are two
calls to destructor.

TestSTL.h:
#include <list
#include #include
using namespace std;

class TestSTL
{
string *_str;

public:
TestSTL() { _str = new string("test"); };
~TestSTL() { cerr << "destruct" << endl; delete _str; }
};


TestSTL.cpp:
#include "TestSTL.h"

int main()
{
list lst.push_back(TestSTL());
}

Could U explain to me what's wrong here?

You failed to define an appropriate copy constructor. The
compiler-generated copy constructor is generally not appropriate for
classes (like yours) that acquire resources (in this case, by using
new).


For the OP:

When the code does push_lst.back, it creates a *copy* of the TestSTL
object you passed (the nameless temporary). Because you didn't specify a
copy constructor, the compiler generated one for you. This compiler-
generated copy constructor does a member-by-member copy (also called
"shallow copy"). So now you have two TestSTL objects, and their _str
members point to the same dynamically allocated string. The object you
created (a temporary) will be destroyed at the end of the push_back
statement, and it will deallocate the string. When the list is destroyed,
it will destroy the stored TestSTL object, which in turn will try to
deallocate the string again.

If you want to store objects in an STL container, it's best to avoid
naked pointer members. If you need a string, you can use std::string. If
you need a lump of memory, you can use std::vector<char>. All STL classes
have proper value semantics; they will take care of the copy operations
that permeate C++.

If you want to keep the naked pointer and still use std::list, you have
two possibilities:

1) Implement the copy constructor and copy assignment operator yourself
to perform a deep copy (allocate another string and copy the original).
This is what std::string does (hint, hint).

2) Do some sort of reference counting.


--
Life is complex, with real and imaginary parts.

Back to top
Puppet_Sock
Guest





PostPosted: Wed Jul 27, 2005 5:50 pm    Post subject: Re: Segmentation fault in list... Reply with quote

saratoga wrote:
Quote:
I've 'Segmentation fault' in this code. I've found that there are two
calls to destructor.
[snip]


Others have given the specific answer to your question.
For this, and many related questions, you should check
out the FAQ for the group.

http://www.parashift.com/c++-faq-lite/
Socks


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.