title: Deleting a pointer in C++
body: <p>Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience.</p>
  
  <p>I've seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a 'simple' pointer (or whatever the proper term might be), here's the code I'm trying to run:</p>
  
  <pre><code>#include &lt;iostream&gt;;
  
  using namespace std;
  
  int main() {
    int myVar,
        *myPointer;
  
    myVar = 8;
    myPointer = &amp;myVar;
  
    cout &lt;&lt; "delete-ing pointers " &lt;&lt; endl;
    cout &lt;&lt; "Memory address: " &lt;&lt; myPointer &lt;&lt; endl;
  
    // Seems I can't *just* delete it, as it triggers an error 
    delete myPointer;
    cout &lt;&lt; "myPointer: " &lt;&lt; myPointer &lt;&lt; endl;
    // Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4:
    // pointer being freed was not allocated
    // *** set a breakpoint in malloc_error_break to debug
    // Abort trap: 6
  
    // Using the new keyword befor deleting it works, but
    // does it really frees up the space? 
    myPointer = new int;
    delete myPointer;
    cout &lt;&lt; "myPointer: " &lt;&lt; myPointer &lt;&lt; endl;
    // myPointer continues to store a memory address.
  
    // Using NULL before deleting it, seems to work. 
    myPointer = NULL;
    delete myPointer;
    cout &lt;&lt; "myPointer: " &lt;&lt; myPointer &lt;&lt; endl;
    // myPointer returns 0.
  
  }
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>Why won't the first case work? Seems the most straightforward use to use and delete a pointer? The error says the memory wasn't allocated but 'cout' returned an address.</li>
  <li>On the second example the error is not being triggered but doing a cout of the value of myPointer <strong>still</strong> returns a memory address?</li>
  <li>Does #3 really work? Seems to work to me, the pointer is no longer storing an address, is this the proper way to delete a pointer?</li>
  </ol>
  
  <p>Sorry for the long question, wanted to make this as clear as possible, also to reiterate, I have little programming experience, so if someone could answer this using layman's terms, it would be greatly appreciated!</p>