Thread: get segmentation fault when I try to delete the pointer to derived class

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    13

    get segmentation fault when I try to delete the pointer to derived class

    Hi

    I created a simple code which has Allocate_memory and Structure_factor class. The later one is is inherited from the former one. I got get segmentation fault when I try to delete the pointer to derived class. Could anyone tell me what is wrong and why,as well as how to change it? I am new to c++, so it is appriciated that someone could give a detailed answers. Thanks

    BYW, code is not finished, so some variables are not neccessary curently.
    Code:
    #include <fstream>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <math.h>
    
    using namespace  std;
    
    //use 3layer no force 11 m/s trajectory to debug
    #define Num_step 10000
    #define Dump_freq  5000
    #define Num_part 3303
    #define Num_bin 25
    #define Max_num_atom_bin 500
    #define Lower_first_bin 10.000
    #define Width_bin 1.0
    
    class Allocate_memory
        {
    protected:
        int num_dump,
             num_part,
             num_coor;
    
    
    public:
        Allocate_memory (int i_dim_x, int i_dim_y, int i_dim_z):num_dump (i_dim_x), num_part (i_dim_y), num_coor (i_dim_z) {};
    
        double *** allocate_memory_3d_array (double *** array_3d)
            {
    
                array_3d=new double ** [num_dump];
                for (int i=0;i<num_dump;i++)
                    {
                        array_3d[i]=new double * [num_part];
                        for (int j=0;j<num_part;j++)
                            {
                                array_3d[i][j]=new double [num_coor];
                            }
                    }
                return array_3d;
            }
    
        double ** allocate_memory_2d_array (double **array_2d)
            {
                array_2d=new double * [num_dump];
                for (int i=0;i<num_dump;i++)
                    {
                        array_2d[i]=new double [num_part];
                    }
    
                return array_2d;
            }
    
        void free_memory_3d_array (double *** array_3d)
            {
                 for (int i=0;i<num_dump;i++)
                     {
                         for (int j=0;j<num_part;j++)
                                  {
                                      delete [] array_3d[i][j];
                                      array_3d[i][j]=NULL;
                                  }
                         delete [] array_3d[i];
                         array_3d[i]=NULL;
                     }
                 delete [] array_3d;
                 array_3d=NULL;
            }
    
        void free_memory_2d_array (double ** array_2d)
            {
                for (int i=0;i<num_dump;i++)
                    {
                        delete [] array_2d[i];
                        array_2d[i]=NULL;
                    }
    
                delete [] array_2d;
                array_2d=NULL;
    
            }
    
    
    
        };
    
    class Structure_factor:public Allocate_memory
        {
    private:
        double *** coor,
                  ** mass;
    
        const string name,
                         output_3d_array,
                         output_2d_array;
    
        string tem,
                mol,
                type,
                x_c,
                y_c,
                z_c,
                i_x,
                i_y,
                i_z,
                s_mass;
    
        fstream lammpstrj;
    
        int dump_freq;
    
    public:
        Structure_factor (const string i_name, const string i_output_3d_array, const string i_output_2d_array, int i_dim_x, int i_dim_y, int i_dim_z, int i_dump_freq):
            Allocate_memory (i_dim_x,i_dim_y,i_dim_z),
            dump_freq (i_dump_freq),
            name (i_name),
            output_3d_array (i_output_3d_array),
            output_2d_array (i_output_2d_array)
        {
            coor=allocate_memory_3d_array (coor);
            mass=allocate_memory_2d_array (mass);
        }
    
        void open_file ()
            {
                 lammpstrj.open (name.c_str(),ios::in);
                 if (!lammpstrj)
                     {
                         cout << "Can not open the flow_solid_Couetteall.lammpstrj" << endl;
                         exit(1);
                     }
                 else
                     {
                         cout << "open the flow_solid_Couetteall.lammpstrj successfully" << endl;
                         read_data ();
                     }
                 lammpstrj.close ();
            }
    
         void read_data ()
             {
                 for (int i=0;i<num_dump;i++)
                     {
                         for (int j=0;j<9;j++)
                             {
                                 getline (lammpstrj,tem);
                             }
                         for (int k=0;k<num_part;k++)
                             {
                                 getline (lammpstrj,mol,' ');
                                 getline (lammpstrj,type,' ');
                                 getline (lammpstrj,x_c,' ');
                                 coor[i][k][0]=strtod(x_c.c_str(),NULL);
                                 getline (lammpstrj,y_c,' ');
                                 coor[i][k][1]=strtod(y_c.c_str(),NULL);
                                 getline (lammpstrj,z_c,' ');
                                 coor[i][k][2]=strtod(z_c.c_str(),NULL);
                                 getline (lammpstrj,i_x,' ');
                                 getline (lammpstrj,i_y,' ');
                                 getline (lammpstrj,i_z,' ');
                                 getline (lammpstrj,s_mass);
                                 mass[i][k]=strtod(s_mass.c_str(),NULL);
                             }
                     }
             }
    
    
         void write_data_3d_array ()
             {
                 lammpstrj.open(output_3d_array.c_str(),ios::out);
                 if (!lammpstrj)
                     {
                         cout <<"Can not open the output for 3d"<<endl;
                         exit (1);
                     }
    
                 else
                     {
                         cout <<"open the output for 3d successfully"<<endl;
                     }
    
                 for (int i=0;i<num_dump;i++)
                     {
                         lammpstrj<<dump_freq*i<<endl;
    
                         for (int k=0;k<num_part;k++)
                             {
                                 lammpstrj<<k+1<<" ";
                                 for (int j=0;j<num_coor;j++)
                                     {
                                         lammpstrj<<coor[i][k][j]<<"";
                                     }
                                 lammpstrj<<endl;
                             }
                     }
    
                 lammpstrj.close ();
                 cout <<"output for 3d is finished"<<endl;
             }
    
         void write_data_2d_array ()
             {
                 lammpstrj.open(output_2d_array.c_str(),ios::out);
                 if (!lammpstrj)
                     {
                         cout << "can not open the output for 2d"<<endl;
                         exit (1);
                     }
    
                 else
                     {
                         cout <<"open the output for 2d successfully"<<endl;
                     }
    
                 for (int i=0;i<num_dump;i++)
                     {
                         lammpstrj<<dump_freq*i<<endl;
    
                         for (int j=0;j<num_part;j++)
                             {
                                 lammpstrj<<j+1<<" ";
                                 lammpstrj<<mass[i][j]<<" "<<endl;
                             }
                     }
                 lammpstrj.close ();
    
             }
    
        ~Structure_factor ()
            {
                free_memory_3d_array (coor);
                free_memory_2d_array (mass);
            }
        };
    
    int main ()
        {
            Structure_factor * structure_factor;
            structure_factor=new Structure_factor ("flow_solid_Couetteall.lammpstrj","coordinate","mass",Num_step/Dump_freq,Num_part,3,Dump_freq);
            structure_factor->open_file ();
            structure_factor->write_data_3d_array ();
            structure_factor->write_data_2d_array ();
            delete [] structure_factor;
        }
    Fan li
    Ph.D student on mechanical engineering

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why does Structure_factor publicly inherit from Allocate_memory? From what I see, Allocate_memory has no virtual functions at all, so it is not intended to be a polymorphic base class. At the very least I would expect a virtual destructor to cater for the possibility of destroying a derived class object through a pointer to Allocate_memory.

    As for your problem: it will take some debugging, methinks. But with all the explicit new and delete in your code, it is quite possible you have something like double deletion going on. I suggest that you read Stroustrup's answer to the FAQ: How do I deal with memory leaks? as it applies here too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Hi laserlight
    Thanks for you help. I solved my problem.
    Regarding your question, I did that inheritance because I want to reuse the code from Allocation_memory class. Also I have just read inheritance from the book, meanwhile my supervisor asked me to programme a post processing code, so I want to practice inference when I programme it. Apparently, from your answer, I think I did a very worse example.

    Fan Li

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2016, 01:15 AM
  2. Replies: 4
    Last Post: 01-24-2016, 05:02 AM
  3. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM

Tags for this Thread