title: Deleting wchar_t* cause heap corruption
body: <p>I've this portion of code:<br></p>
  
  <pre><code>class CNuovoVocabolo {
      CString m_strItaliano;
      CString m_strRusso;
      //...
  }
  
  void CNuovoVocabolo::GetVocabolo( CVocabolo* pVocabolo )
  {
      int nLIta;
      int nLRus;
      wchar_t *pIta;
      wchar_t *pRus;
  
      nLIta = m_strItaliano.GetLength();
      nLRus = m_strRusso.GetLength();
      if( nLIta > 0 && nLRus > 0 )
      {
          pIta = new wchar_t[nLIta + 1];
          wcscpy_s( pIta, nLIta * sizeof( wchar_t ), m_strItaliano );
          pRus = new wchar_t[nLRus + 1];
          wcscpy_s( pRus, nLRus * sizeof( wchar_t ), m_strRusso );
          pVocabolo->SetVocabolo( pIta, nLIta, pRus, nLRus );
          delete [] pIta; // *1
          delete [] pRus; // *2
      }
  }
  
  // Inside CVocabolo::SetVocabolo:
  void CVocabolo::SetVocabolo( const wchar_t* pIta/*=NULL*/, unsigned short nLIta/*=0*/, const wchar_t* pRus/*=NULL*/, unsigned short nLRus/*=0*/ )
  {
      if( pIta != NULL && pRus != NULL && nLIta > 0 && nLRus > 0 )
      {
          AzzeraVocabolo();
          m_nLIta = nLIta;
          m_nLRus = nLRus;
          m_pItaliano = new wchar_t[nLIta + 1];
          wcscpy_s( m_pItaliano, nLIta * sizeof( wchar_t ), pIta );
          m_pRusso = new wchar_t[nLRus + 1];
          wcscpy_s( m_pRusso, nLRus * sizeof( wchar_t ), pRus );
      }
  }</code></pre>
  
  <p>When I delete pIta pointer and pRus pointer at *1 and *2 I get a crash because of heap corruption. Honestly I can't understand why this is happening and where I'm doing wrong. Any suggestions?</p>
  
  <p><b>EDIT:</b><br>
  Looking on the Net, I've found my solution:<br></p>
  
  <pre><code>pIta = new wchar_t[( nLIta + 1 ) * sizeof( wchar_t )];</code></pre>
  
  <p>But to be honest, it's unclear for me. If you can give me an explaination...
  Also, knowing why the question got downvoted would be nice.</p>