1

正如标题所说,我知道 new 抛出一个可以被捕获的异常,但是指针到底发生了什么?它变成NULL?我检查了一些关于 SO 的答案,但没有人解释它。检查下面的示例,指针保持在堆上?请提供有关此模式的完整信息

#include <windows.h>
#include <cstdlib>
#include <iostream>

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 << szBuffer;
        }
    }
    else
    {
        char szBuffer[1024];
        while( fgets( szBuffer, 1024, m_File ) )
        {
               uFindNumber++;
               if( uFindNumber == uLine )
               {
                   std::cout << 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->IsValidFile() )
    {
        pFile->PrintFile(2);
    }

    CFileStatic *pConsoleCpp = new CFileStatic( "Console.cpp", READ_WRITE );
    if( pConsoleCpp->IsValidFile() )
    {
        pConsoleCpp->PrintFile();
    }


    system("pause>nul");
    return EXIT_SUCCESS;
}
4

4 回答 4

6

假设您使用的是默认全局new运算符,并且没有使用以下nothrow版本new

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 );
  /* ... */
}

没有“以后”。在这段代码中,如果new失败,它会抛出一个std::bad_alloc异常,你的程序会立即终止。

现在,如果您要尝试处理这种情况,则需要捕获该异常。或许是这样的:

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& ex )
  {
    cout << "whoops! out of memory." << ex.what() <<  endl;
  }
  /* ... */
}

pFile存在于块所包围的范围内try,因此不再存在。移出一层:

  CFileStatic * pFile = 0;
  try
  {
    pFile = new CFileStatic( "Console.h", READ_WRITE );
  }
  catch( const std::bad_alloc& ex )
  {
    cout << "whoops! out of memory." << ex.what() <<  endl;
  }

  // pFile is still 0

pFile永远不会被修改,所以值是不变的。


关于delete指针的问题。标准说(C++03 5.3.5/2):

[...] 如果 delete 的操作数的值是空指针,则操作无效。

您不必删除 NULL 指针,因为没有 to delete,但这样做不会有任何效果。你可以安全地这样做:

CFileStatic * pFile = 0;
try
{
  pFile = new CFileStatic( "Console.h", READ_WRITE );
}
catch( const std::bad_alloc& ex )
{
  cout << "whoops! out of memory." << ex.what() <<  endl;
}

delete pFile;  // regardless of the success of new, this is OK

请注意,在执行此操作时,初始化pFile为空指针尤为重要,正如我在此处所做的那样。如果您不这样做:

CFileStatic* pFile; // NO INIT
/* ... */
delete pFile; // if new threw, this is undefined behavior

pFile仍然是一个垃圾指针。它不是空指针,所以delete会尝试删除它,导致未定义的行为。

于 2012-06-18T19:52:07.183 回答
3

当函数抛出异常时,该函数没有完成并且不返回任何内容,同时控制流跳转到异常处理程序,即使函数返回,也不会执行对接收指针的赋值。

的抛出版本也是new如此,如果它抛出,那么指针将保持它在表达式之前的相同值,它可能是 NULL 或任何其他值。

另一方面,如果您使用new (std::nothrow) X;then 调用new将不会抛出,并且失败将由返回的 NULL 值指示(并假设分配给指针)

于 2012-06-18T19:53:47.270 回答
2

如果抛出异常,则不会为指针分配新值。如果您的指针位于try您进行分配的块内,或者不在任何try块内,则异常将导致范围更改,从而您无法再访问指针。因此,无需测试指针。

另一方面,如果指针以在分配失败后仍然存在的方式声明,那么您需要检查该值。例如:

T* ptr = NULL;
try {
    ptr = new T();
} catch (std::bad_alloc& ) {
    /* ... handle error */
}
// Have to test ptr here, since it could still be NULL.

希望这可以帮助!

于 2012-06-18T19:53:12.363 回答
1

如果new失败,则抛出std::bad_alloc异常,除非您使用该nothrow版本。这意味着存储返回指针的变量的值将保持不变。

如果您捕获异常,则值pFile保持未初始化,否则您的应用程序将终止(如main已退出)

于 2012-06-18T19:51:27.163 回答