title: C++ Copy Constructor - Deep Copy of Pointers Segmentation Fault
body: <p>I recently began learning C++ via Advanced Computer Science Fundamentals MOOC.  We have a challenge that declares in a hidden .h file (cannot change) and we need to code all of the member functions/constructors/destructor initialized in the .h file.  </p>
  
  <p>To avoid cheating or anyone doing the work for me, I would just appreciate if anyone can help explain what I am doing wrong and what I am doing to get a segmentation fault.  I've searched and searched all deep vs shallow copy guides but can't seem to understand what I am doing wrong with this.  </p>
  
  <p>Here is the .h file:</p>
  
  <pre><code>  class Pair {
      public:
        int *pa,*pb;
        Pair(int, int);
        Pair(const Pair &amp;);
        ~Pair();
     };
  </code></pre>
  
  <p>Two things to point out from this is that the pa/pb are pointers to ints rather than just ints and that there is no place for an assignment operator even though I read about the "Rule of the Big Three" that explained if I had copy constructor or destructors I should also have assignment operator.  </p>
  
  <p>Moving along, I tried many things to get this to work and included tons of diagnostic messages and can see sort of where I am messing up but can't understand <em>why</em> or what I <em>should</em> be doing.  </p>
  
  <p><strong>Custom Constructor</strong>
  I think this is okay:</p>
  
  <pre><code> Pair::Pair (int a,int b) {
        int *pa = new int(a);
        int *pb = new int(b);
        std::cout &lt;&lt; "pa points to value : " &lt;&lt; *pa &lt;&lt;std::endl;
        std::cout &lt;&lt; "pb points to value : " &lt;&lt; *pb &lt;&lt;std::endl;
        std::cout &lt;&lt; "custom constructor resolved "&lt;&lt;std::endl;
      }
  
  </code></pre>
  
  <p><strong>Copy Constructor</strong>
  Please forgive all the comments I was just trying to troubleshoot.</p>
  
  <pre><code>    Pair::Pair(const Pair &amp;obj) {
  
      int *const * a = &amp;obj.pa;
      int *const * b = &amp;obj.pb;
      int *pa = new int;
      int *pb = new int;
      pa = *a;
      pb = *b;
  
      std::cout &lt;&lt; "obj.pa address is : " &lt;&lt; &amp;obj.pa &lt;&lt;std::endl;
      std::cout &lt;&lt; "obj.pb address is : " &lt;&lt; &amp;obj.pb &lt;&lt;std::endl;
      std::cout &lt;&lt; "obj.pa points at : " &lt;&lt; obj.pa &lt;&lt;std::endl;
      std::cout &lt;&lt; "obj.pb points at : " &lt;&lt; obj.pb &lt;&lt;std::endl;
  
      std::cout &lt;&lt; "pa address is : " &lt;&lt; &amp;pa &lt;&lt;std::endl;
      std::cout &lt;&lt; "pb address is : " &lt;&lt; &amp;pb &lt;&lt;std::endl;
      std::cout &lt;&lt; "pa is pointing at : " &lt;&lt; pa &lt;&lt;std::endl;
      std::cout &lt;&lt; "pb is pointing at : " &lt;&lt; pb &lt;&lt;std::endl;
      std::cout &lt;&lt; "copy constructor called "&lt;&lt;std::endl;
       }
  </code></pre>
  
  <p><strong>Destructor</strong>
  I think I did okay on this one:</p>
  
  <pre><code>    Pair::~Pair() {
        delete pa; pa = nullptr;
        std::cout &lt;&lt; "pa deleted " &lt;&lt; std::endl;
  
        delete pb; pb = nullptr;
        std::cout &lt;&lt; "pb deleted " &lt;&lt; std::endl;
      }
  </code></pre>
  
  <p><strong>Main</strong>
  Series of tests</p>
  
  <pre><code>int main() {
    Pair p(15,16);
    Pair q(p);
    Pair *hp = new Pair(23,42);
    delete hp;
  
    std::cout &lt;&lt; "If this message is printed,"
      &lt;&lt; " at least the program hasn't crashed yet!\n"
      &lt;&lt; "But you may want to print other diagnostic messages too." &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>Looking at this the program would create a pair p; then create a deep copy pair q; then create a pointer to pair hp that points to a pair 23,42 on the heap; and then deletes pointer to pair hp.  Everything seems to compile and run properly except for the copy constructor.  The main issue is that &amp;obj.pa seems to be just pulling the address that the pointer *pa of object obj is pointing to rather than retrieving the actual dereferenced value.  </p>
  
  <p>For additional information here is the terminal output when executing .main (adding additional commentary):  </p>
  
  <pre><code>pa points to value : 15            // pair p.pa correct
  pb points to value : 16            // pair p.pb correct
  custom constructor resolved        // pair p made
  obj.pa address is : 0x7fff1887c780 // address original object pointer pa stored 
  obj.pb address is : 0x7fff1887c788 // address original object pointer pb stored
  obj.pa points at : 0x2             // address pointer obj pa is directed to (I want the value not this)
  obj.pb points at : 0x40102d        // address pointer obj pb is directed to (I wand the value not this)
  pa address is : 0x7fff1887c728     // pa address on stack
  pb address is : 0x7fff1887c730     // pb address on stack
  pa is pointing at : 0x2            // address value copied not address itself
  pb is pointing at : 0x40102d       // address value copied not address itself
  copy constructor called            // copy constructor runs through albeit incorrect
  pa points to value : 23            // hp.pa
  pb points to value : 42            // hp.pb
  custom constructor resolved        // constructor made on heap from pointer hp
  pa deleted deleted original pa     // (made from pointer to pair hp)
  pb deleted deleted original pa     // (made from pointer to pair hp)
  If this message is printed, at least the program hasn't crashed yet!
  But you may want to print other diagnostic messages too.
  pa deleted                         // deleted original pa (made from pair p)
  pb deleted                         // deleted original pb (made from pair p)
  </code></pre>
  
  <p>I run through and after all execution get 'Segmentation Fault' which I can't quite understand.  And being so early in C++ I am not even confident in my terminology to help my searching ability.</p>