title: Delete on a pointer in a Union
body: <p>I have tried some interesting code(at least for me !). Here it is.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct myStruct{
      int one;
  
      /*Destructor: Program crashes if the below code uncommented*/
      /*
      ~myStruct(){
          std::cout&lt;&lt;"des\n";
      } 
      */
  };
  struct finalStruct {
      int noOfChars;
      int noOfStructs;
  
      union { 
          myStruct *structPtr;
          char *charPtr;
      }U;
  
  };
  int main(){
      finalStruct obj;
      obj.noOfChars = 2;
      obj.noOfStructs = 1;
      int bytesToAllocate = sizeof(char)*obj.noOfChars 
                          + sizeof(myStruct)*obj.noOfStructs;
  
      obj.U.charPtr = new char[bytesToAllocate];
      /*Now both the pointers charPtr and structPtr points to same location*/
  
      delete []obj.U.structPtr;
  }
  </code></pre>
  
  <p>I have allocated memory to charPtr and deleted with structPtr. It is crashing when I add a destructor to myStruct otherwise no issues.</p>
  
  <p>What exactly happens here. As I know delete[] will call the destructor as many times as number given in new[]. Why it is not crashing when there is no destructor in myStruct?</p>