title: What difference it's making when statically allocated memory for pointer vs dynamically allocated memory for pointer deletion?
body: <pre><code>#include&lt;iostream&gt;
  using namespace std;
  class A
  {
     int x;
     int *ptr;
     int *ptr2;
     public:
     A(int y)
     {
       cout&lt;&lt;"para const"&lt;&lt;endl;
       x=y;
       ptr=&amp;x;           //Static allocation
       ptr2=new int;     //Dynamic allocation
       (*ptr2)=y;
       cout&lt;&lt;"ptr "&lt;&lt;&amp;ptr&lt;&lt;"--"&lt;&lt;ptr&lt;&lt;"--"&lt;&lt;*ptr&lt;&lt;endl;
       cout&lt;&lt;"ptr2 "&lt;&lt;&amp;ptr2&lt;&lt;"--"&lt;&lt;ptr2&lt;&lt;"--"&lt;&lt;*ptr2&lt;&lt;endl;
     }
     void show()
     {
       cout&lt;&lt;x&lt;&lt;"--"&lt;&lt;(*ptr)&lt;&lt;"--"&lt;&lt;(*ptr2)&lt;&lt;endl;
     }
     ~A()
     {
       cout&lt;&lt;"dest"&lt;&lt;endl;
       //delete ptr;    //will leads to segmentation fault
       delete ptr2;
     }
  };
  int main()
  {
     A obj1(10);
     obj1.show();
     A obj2(20);
     obj2.show();
     cout&lt;&lt;&amp;obj1&lt;&lt;endl;
     cout&lt;&lt;&amp;obj2&lt;&lt;endl;
  }
  </code></pre>
  
  <p><strong>OutPut:</strong></p>
  
  <pre><code>para const
  ptr 0x7ffe27dedbc8--0x7ffe27dedbc0--10
  ptr2 0x7ffe27dedbd0--0x2132010--10
  10--10--10
  para const
  ptr 0x7ffe27dedba8--0x7ffe27dedba0--20
  ptr2 0x7ffe27dedbb0--0x2132030--20
  20--20--20
  0x7ffe27dedbc0
  0x7ffe27dedba0
  dest
  dest
  </code></pre>
  
  <p>I can see for both the object, the pointers (1. statically allocated stored in stack and 2. dynamically allocated stored in heap) are created in separate memory &amp; holding also separate memory . Then why deletion of ptr leads to segmentation fault and not for ptr2. I believe swallow copy &amp; deep copy is not making any impact here as I am not copying the objects to one another.
  And also one more thing I am observing is the address of obj1 &amp; obj2 is same as address of ptr &amp; ptr2 , why so ? </p>
  
  <p>Can someone explain me this behavior please ?</p>