Segmentation fault for different ways of initializing class

Greetings. I define a class, D, one of whose members is a pointer to a class, E. The class E has two subclasses, E1 and E2. When I run the first code shown below, in which a D variable is declared and initialized in different statements, I get a segmentation fault. When I run the second code shown below, in which a D variable is declared and initialized in statement, I get the correct behavior. Can someone please tell me why, and how I can change the first code so that the D variable is declared and initialized in different statements but still runs correctly? Thanks.

First code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::string;
using std::vector;

class E {
   public:
      E() {}

      virtual ~E() {}

      virtual void Process_x(const vector<int>& n) = 0;

   protected:
      void Process_c_x(const vector<int>& nc) {}
};

class E1 : public E {
   public:
      E1() {}

      ~E1() {}

      void Process_x(const vector<int>& nc)
      {
         E::Process_c_x(nc);
      }
};

class E2 : public E {
   public:
      E2() {}

      ~E2() {}

      void Process_x(const vector<int>& nc)
      {
         E::Process_c_x(nc);
      }
};

class D {
   public:
      D(const vector<int>& a, int b)
      {
         if (b == 1)
            p = new E1();
         else
            p = new E2();
      }

      D() {}

      ~D() {delete p;}

      void Process_x(const vector<int>& n)
      {
         p->Process_x(n);
      }

   private:
      E *p;
};

int main(int argc, char **argv)
{
   D d;

   vector<int> a;
   a.push_back(1);

   int b = 2;

   d = D(a, b);
}



Second code (only main program shown; all other code is the same)
1
2
3
4
5
6
7
8
9
10
11
...

int main(int argc, char **argv)
{
   vector<int> a;
   a.push_back(1);

   int b = 2;

   D d(a, b);
}
Situation one is not possible in C++. Also consider the fact that the constructor is a special member function without a return type.

Aceix.
You should better initialize all attributes of your object to avoid accessing some undefined value like in your destructor.
D() : p() {}
and better have a look at p before trying to delete a null pointer.
1
2
3
4
5
~D()
{
    if (p != std::nullptr)
        delete p;
}
> Situation one is not possible in C++
¿why not?


@OP: rule of three. If you need to code a destructor, a copy constructor or an assignment operator, you probably need the three.
You are trying to delete the same memory twice


> look at p before trying to delete a null pointer.
unnecessary. deleting a null pointer does nothing
Last edited on
@ne555 WOW

Aceix.
Hello again. I tried the modifications by tcs, and I still get a segmentation fault. By the way, the correct
syntax is if (p != nullptr) ...

Also, to ne555, can you tell me how I am trying to delete the same memory twice? I don't understand.
Thanks.

Specific code for getting the program to work would be most welcome. Thanks again.
Line 78 copies D(a, b) to d. Now you've two objects (D(a, b) and d) referencing the same E. At first D(a, b) was destroyed due to its temporary nature which in turn destroys the E. Finally when leaving main, d will be destroyed deleting E another time.
Topic archived. No new replies allowed.