|
-
August 10th, 2011, 01:44 AM
#1
Segmentation Fault using STL
Hi All,
I am facing problem. I am getting Segmentation fault in the following code.
// Store a class object in a vector.
#include <iostream>
#include <vector>
using namespace std;
class Parent{
int id;
public:
Parent(){};
Parent(int x){ id=x;}
virtual ~Parent(){ cout<<"Parent"<<endl;}
virtual void print3(){cout<<"Printing Parent "<<id;}
};
class Child ublic Parent{
int c;
public:
Child(int m,int n):Parent(m){
c=n;
}
Child(){c=0;}
virtual ~Child(){ cout<<"Child"<<endl;}
virtual void print3(){cout<<"Printing Child "<<c;}
};
class New_class
{
public:
New_class(){
tp=new Child(10,20);
}
~New_class(){
delete tp;
}
void check(Parent &tmp){
tmp.print3();
}
void print2(){tp->print3();}
private:
Parent *tp;
};
class New2{
vector<New_class> tp2;
public:
New2(){
tp2.push_back(New_class());
}
~New2(){
tp2.clear();
}
void print(){ vector<New_class>::iterator it=tp2.begin(); (*it).print2();}
};
int main()
{
New2 m ;
m.print();
}
Thanks in advance.
Regards
-
August 10th, 2011, 02:08 AM
#2
Re: Segmentation Fault using STL
please, use code tags [ code ] ... [ /code ] as your code is barely readable;
anyway, I can see at least two problems:
1) the New_class has no properly defined copy-constructor ( or move-constructor, and-or (move)assignment operator ), which is also probably causing the segfault: in the line "tp2.push_back(New_class());" you are copying an instance of New_class in the vector; the to-be-copyed class is destroyed, it's internally managed pointer to Child is deleted and the copyed instance stored in the vector remains with a dangling invalid pointer, rising a segfault later in the "(*it).print2();" line.
2) storing polymorphic objects in STL containers is asking for troubles, it's better to make them non-copyable and store (smart) pointers to them instead ...
-
August 10th, 2011, 02:08 AM
#3
Re: Segmentation Fault using STL
try it like this:
Code:
void print()
{
vector<New_class>::iterator it;
for( it = tp2.begin() ; it < tp2.end(); it++ )
{
count << *it;
}
}
-
August 10th, 2011, 05:11 AM
#4
Re: Segmentation Fault using STL
 Originally Posted by deepakpengoria
Hi All,
I am facing problem. I am getting Segmentation fault in the following code.
The issue is that your class is not safely copyable, and vector requires your class to have safe and proper copy semantics. In general, your problem has really nothing to do with vector at all and everything to do with your class not being safely copyable.
The following code that doesn't use any vectors shows the same problem:
Code:
class foo
{
int *ptr;
public:
foo() { ptr = new int [10]; }
~foo() { delete [] ptr; }
};
int main()
{
foo f;
foo f2 = f;
foo f3;
f3 = f2;
}
The vector class makes the same or similar assignments when making copies of an object.
The foo class would also have problems if stored in a vector, and the reason is what you see in the main() program. Copies of the object are made, and copying the object is not safe. The simple code above has a double delete problem and a memory leak when main() exits. You have the same problem if you stored foo's in a vector<foo>.
The way you fix this is to write a proper copy constructor and assignment operator that handles the pointer member. You didn't do that in your New_class class.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|