如果新的失败,是否需要检查指针的有效性? [英] Is it required to check a pointer validity if new fails?

查看:79
本文介绍了如果新的失败,是否需要检查指针的有效性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

#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;
}

推荐答案

当从函数中抛出异常时,该函数不会完成并且不返回任何内容,同时控制流会跳转到异常处理程序,甚至如果该函数返回,则不会执行对接收指针的赋值.

When an exception is thrown out of a function the function does not complete and does not return anything, at the same time control flow jumps to the exception handler and even if the function returned, the assignment to the receiving pointer would not be executed.

new的抛出版本也是如此,如果抛出,则指针将保持与表达式之前相同的值,该值可能为NULL或任何其他值.

The same goes for the throwing version of new, if it throws, then the pointer would maintain the same value it had before the expression, which might be NULL or any other value.

另一方面,如果使用new (std::nothrow) X;,则不会引发对new的调用,并且将通过返回NULL值(并假定已将其分配给指针)来指示失败

On the other hand, if you use new (std::nothrow) X; then the call to new will not throw, and failure will be indicated by a NULL value being returned (and assumingly assigned to a pointer)

这篇关于如果新的失败,是否需要检查指针的有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆