为何内存占用正常 new 也会失败?

xengine-qyt 2015-01-16 11:55:59
m_saCache = new(std::nothrow) BYTE[CACHE_SIZE];
if (m_saCache == NULL)
return wlet(FALSE, L"new cache is null.");


这是怎么回事? 搞不懂。。。
...全文
289 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xengine-qyt 2015-01-16
  • 打赏
  • 举报
回复
不是 是VS2013 UP4,的确看出来 所以我想问问出了内存不够申请失败还会有什么情况让他失败?
乔巴好萌 2015-01-16
  • 打赏
  • 举报
回复
你是VC6.0吗 stackoverflow上有人讨论过 up vote 37 down vote accepted VC6 was non-compliant by default in this regard. VC6's new returned 0 (or NULL). Here's Microsoft's KB Article on this issue along with their suggested workaround using a custom new handler: Operator new does not throw a bad_alloc exception on failure in Visual C++ If you have old code that was written for VC6 behavior, you can get that same behavior with newer MSVC compilers (something like 7.0 and later) by linking in a object file named nothrownew.obj. There's actually a fairly complicated set of rules in the 7.0 and 7.1 compilers (VS2002 and VS2003) to determine whether they defaulted to non-throwing or throwing new. It seems that MS cleaned this up in 8.0 (VS2005)—now it always defaults to a throwing new unless you specifically link to nothrownew.obj. Note that you can specify that you want new to return 0 instead of throwing std::bad_alloc using the std::nothrow parameter: SomeType *p = new(std::nothrow) SomeType; This appears to work in VC6, so it could be a way to more or less mechanically fix the code to work the same with all compilers so you don't have to rework existing error handling.
falloutmx 2015-01-16
  • 打赏
  • 举报
回复
碎片化得太厉害?你先重启下机器,重新建立个项目把代码拷过去,然后试试
此后三年 2015-01-16
  • 打赏
  • 举报
回复
光这样看,new没问题。。。

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧