CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2011
    Posts
    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 Childublic 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

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    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 ...

  3. #3
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    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; 
       }
    }
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Segmentation Fault using STL

    Quote Originally Posted by deepakpengoria View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured