Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you are using the default global <code>new</code> operator, and have not used the <code>nothrow</code> version of <code>new</code>:</p> <pre><code>int main( int argc, char *argv[] ) { //if new fails, what 'pFile' turns out to be? and do I need to delete //it later? CFileStatic *pFile = new CFileStatic( "Console.h", READ_WRITE ); /* ... */ } </code></pre> <p>There is no "later". In this code, if <code>new</code> fails, it will throw an <code>std::bad_alloc</code> exception, and your program will terminate immediately.</p> <p>Now, if you were to try to handle this situation, you would need to catch that exception. Perhaps like this:</p> <pre><code>int main( int argc, char *argv[] ) { //if new fails, what 'pFile' turns out to be? and do I need to delete //it later? try { CFileStatic *pFile = new CFileStatic( "Console.h", READ_WRITE ); } catch( const std::bad_alloc&amp; ex ) { cout &lt;&lt; "whoops! out of memory." &lt;&lt; ex.what() &lt;&lt; endl; } /* ... */ } </code></pre> <p><code>pFile</code> exists within the scope enclosed by the <code>try</code> block, so it no longer exists. Moving out one level:</p> <pre><code> CFileStatic * pFile = 0; try { pFile = new CFileStatic( "Console.h", READ_WRITE ); } catch( const std::bad_alloc&amp; ex ) { cout &lt;&lt; "whoops! out of memory." &lt;&lt; ex.what() &lt;&lt; endl; } // pFile is still 0 </code></pre> <p><code>pFile</code> is never modified, so the value is unchanged.</p> <hr> <p>Regarding the question of <code>delete</code>ing the pointer. The Standard says (C++03 5.3.5/2):</p> <blockquote> <p>[...] if the value of the operand of delete is the null pointer the operation has no effect.</p> </blockquote> <p>You do not have to delete a NULL pointer, because there is nothing to <code>delete</code>, but doing so will have no effect. You could safely do this:</p> <pre><code>CFileStatic * pFile = 0; try { pFile = new CFileStatic( "Console.h", READ_WRITE ); } catch( const std::bad_alloc&amp; ex ) { cout &lt;&lt; "whoops! out of memory." &lt;&lt; ex.what() &lt;&lt; endl; } delete pFile; // regardless of the success of new, this is OK </code></pre> <p>Note that when doing this, it is especially important that you initialize <code>pFile</code> to the null pointer, as I've done here. If you don't:</p> <pre><code>CFileStatic* pFile; // NO INIT /* ... */ delete pFile; // if new threw, this is undefined behavior </code></pre> <p><code>pFile</code> will still be a garbage pointer. It's not the null pointer, so <code>delete</code> will try to delete it, resulting in Undefined Behavior.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload