各位大侠,请看下面的语句:
   char *p = NULL;
   try
   {
    p = new char[10];
    if (p == NULL)
       return;
   }
   catch(...)
   {
       if (p != NULL)
         delete p;
       return;
   }  我想问一下,new时如果发生问题,p的值是保持NULL,还是变为vc默认的0xcdcdcdcd ?
  因为如果变为0xcdcdcdcd,那我在catch中就会发生delete p的错误,而导致程序出问题了。
  这是我商业程序中的问题,请一定非常清醒再回答,不太清楚的人万勿回答,谢谢。

解决方案 »

  1.   

    问问题前,首先自己要动脑子想。
    不知道new是不是返回NULL,那么你就首先应该想到去MSDN查帮助,然后是通过网站搜索相关主题,看别人是怎么解决的。
    请一定非常清醒再回答,不太清楚的人万勿回答--不要这么样说话,听了不舒服。
    看你这个问题前,我也不知道是不是返回NULL。因此查MSDN。对new operator的说明中有这么一段:
    If there is insufficient memory for the allocation request, by default operator new returns NULL. 
    也许对你有帮助。
    但我没有实验,所以我不正面回答了。
      

  2.   

    对不起,如果我的帖子有让某些朋友感觉不舒服的地方,算小弟得罪了。是因为上次发了一个帖子,有些其实不懂的人乱发言,结果不仅不好,反而造成浪费了大量时间。是这样的,我注意到当VC执行某句失败时,会自动跳到catch中,而不是返回NULL,这一点是我调试其他语句时发现的。所以这里同时加入了if (p == NULL)判断和catch错误处理。期待高手指点。
      

  3.   

    还是MSDN上的:
    /* HANDLER.CPP: This program uses _set_new_handler to 
     * print an error message if the new operator fails.
     */#include <stdio.h>
    #include <new.h>/* Allocate memory in chunks of size MemBlock. */
    const size_t MemBlock = 1024;/* Allocate a memory block for the printf function to use in case
     * of memory allocation failure; the printf function uses malloc.
     * The failsafe memory block must be visible globally because the
     * handle_program_memory_depletion function can take one 
     * argument only.
     */
    char * failsafe = new char[128];/* Declare a customized function to handle memory-allocation failure.
     * Pass this function as an argument to _set_new_handler.
     */
    int handle_program_memory_depletion( size_t );void main( void )
    {
       // Register existence of a new memory handler.
       _set_new_handler( handle_program_memory_depletion );
       size_t *pmemdump = new size_t[MemBlock];
       for( ; pmemdump != 0; pmemdump = new size_t[MemBlock] );
    }int handle_program_memory_depletion( size_t size )
    {
       // Release character buffer memory.
       delete failsafe;
       printf( "Allocation failed, " );
       printf( "%u bytes not available.\n", size );
       // Tell new to stop allocation attempts.
       return 0;
    }
      

  4.   

    在MSDN上,没有看到new操作会抛出异常的说明。
      

  5.   

    仍是NULL,因為一但丟出例外會發生stack unwinding ,直到有任何catch 來接收,否則最終
    程序呼叫terminate()結束執行.除非你一開始char *p = NULL; 改為 char *p; p的此時值才是compiler給的0xcdcdcdcd,
    所以丟出例外才是0xcdcdcdcd,若以你的例子是NULL.以 dynamic_cast 為例:1. 當以pointer轉型時,失敗是回傳null,不丟出例外(記的開啟rtti)
    2. 當以reference 轉型時,失敗是丟出例外(記的開啟rtti)ex:
    #include<iostream>
    #include <exception>
    using namespace std;
    class A
    {
       public:
       virtual void abc(){} 
    };
    class B:public A
    {
    };
    int main()
    { A AA;
    try
    {
     B& BB=dynamic_cast<B&>(AA); //故意不當轉型,丟出例外
     printf("TEST"); //當上面丟出例外,直接跳過此行,也表示了BB 事實上referennce
                              //所給的預設值0xcccccccc
    }
    catch(exception & ex)
    {
    cout<<ex.what();//顯示 Bad dynamic_cast!
                      // 當執行到此,為了再次確定,去查看BB 的位址,仍然還是0xcccccccc.
    }
    }
      

  6.   

    谢谢楼上的朋友。我想再请问,new错误处理过程在debug和release版中,有何不同么?
    我现在是debug版。
      

  7.   

    debug: 不會最佳化,例如使用inline 它會當成一般函式使用, 含有除錯資訊,利於debugrelease: 能最佳化就最佳化, 不含debug 資訊基本上只有如此差異罷了.須小心的是,因為release 不含 debug 資訊,所以 當 char a;沒指派任何值時, 它的值是不固定的,並非compiler 所給之值 0xcccccccc.所以也就增加除錯上困難,盡量還是用debug,當沒問題在弄成release 即可
      

  8.   

    现在我的系统运行时,有一部分线程中断运行了,可还有一个线程仍在运行。我所有的代码都放在了try...catch里,只有上述这一个地方在catch中对p进行判断,并用了delete操作,这是我现在担心的问题。
    我很奇怪的是,有的线程和其他的线程一点关系也没有,为什么也会停下来。是不是VC一个线程出问题,其他的线程也会停下来呢?
      

  9.   

    最好catch(...) 改成 單獨可以接受各個錯誤處理,因為說不定不是new 所丟出的錯誤.現在疑惑的是, new 記憶體配置不足 ,應該是回傳null ,好像不丟出錯誤 .new 不可能內存配置失敗,會回傳null和丟出錯誤,2者無法同時進行. 假若就算new 丟出例外,delete p是多餘的,因為new 根本沒配置成功,多此一行反而是刪掉之前所配置成功內存,所以有可能影響到有使用此內存的其它線程.