Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it required to check a pointer validity if new fails?
    primarykey
    data
    text
    <p>As title says, i know that new throws an exception which can be caught, but what exactly happens to the pointer? it turns NULL? I checked some answers on SO but none explained it. Check example below, the pointer keeps on the heap? please give full info on this pattern</p> <pre><code>#include &lt;windows.h&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; using namespace std; enum eReadMode { // READ_ONLY, READ_WRITE, // CREATE_FILE, // CREATE_WRITE_FILE, }; class CFileStatic { private: FILE *m_File; public: CFileStatic( LPCTSTR szFileName, eReadMode eMode ); virtual ~CFileStatic() {}; bool IsValidFile() const { return( m_File != NULL ); }; void PrintFile( unsigned int uLine = 0 ); }; CFileStatic::CFileStatic( LPCTSTR szFileName, eReadMode eMode ) { if( szFileName ) { if( eMode == READ_WRITE ) m_File = fopen( szFileName, "r+" ); else printf( "Valid usage of: READ_WRITE only" ); } else m_File = NULL; } void CFileStatic::PrintFile( unsigned int uLine ) { static unsigned uFindNumber; if( uLine == 0 ) { char szBuffer[1024]; while( fgets( szBuffer, 1024, m_File ) ) { std::cout &lt;&lt; szBuffer; } } else { char szBuffer[1024]; while( fgets( szBuffer, 1024, m_File ) ) { uFindNumber++; if( uFindNumber == uLine ) { std::cout &lt;&lt; szBuffer; } } } } 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 ); if( pFile-&gt;IsValidFile() ) { pFile-&gt;PrintFile(2); } CFileStatic *pConsoleCpp = new CFileStatic( "Console.cpp", READ_WRITE ); if( pConsoleCpp-&gt;IsValidFile() ) { pConsoleCpp-&gt;PrintFile(); } system("pause&gt;nul"); return EXIT_SUCCESS; } </code></pre>
    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.
 

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