new && new(std::nothrow)
2015-03-17 17:06
253 查看
new && new(std::nothrow)
转载▼
源码方面的信息就不详细讲述了,找找代码看看便知,简要说明下:
new(std::nothrow) 顾名思义,即不抛出异常,当new一个对象失败时,默认设置该对象为NULL,这样可以方便的通过if(p == NULL) 来判断new操作是否成功
普通的new操作,如果分配内存失败则会抛出异常,虽然后面一般也会写上if(p == NULL) 但是实际上是自欺欺人,因为如果分配成功,p肯定不为NULL;而如果分配失败,则程序会抛出异常,if语句根本执行不到。
因此,建议在c++代码中,凡是涉及到new操作,都采用new(std::nothrow),然后if(p==NULL)的方式进行判断
相关文章推荐
- new && new(std::nothrow)
- new(std::nothrow) 和 new
- new(std::nothrow)
- new 和 new(std::nothrow)
- 关于 new(std::nothrow)
- new(std::nothrow) 应用场景
- could be 'void *operator new(size_t,const std::nothrow_t &) throw()'
- new(std::nothrow) 和 new() 的区别
- new(std::nothrow) 应用场景
- 在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NULL
- new (std::nothrow) 与 new
- new (std::nothrow) 与 new
- 自定义std::sort的比较函数时发生"invalid operator<"错误原因
- NEW '_time' obfuscation area in FLEXlm v10 by roli_bark
- netbeans 7 DO NOT support ruby & found a new ide http://redcareditor.com/ for ruby
- 大量数据文件恢复时,set new…
- 不能将参数 1 从“int”转换为“std::_Vector_const_iterator<_Myvec>”(VS2010 )
- Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
- 关于VC中 error C2871: 'std' : does not exist or is not a namespace
- The method execute(Void[]) is ambiguous for the type new AsyncTask<Void,Void,Void>(){}