Windows Mobile 在“new”失败时返回 0
我有一个 Visual Studio 2008 Windows Mobile 6.5.3 ARMV4I 项目。我使用 try/catch 块检查内存不足情况,查找 std::bad_alloc 异常。但是,通过测试我发现它实际上可以只返回 NULL 值而不抛出异常。
int _tmain( int argc, _TCHAR* argv[] )
{
int i = 0;
try
{
for( ; i < 30000; ++i )
{
BYTE* f = new BYTE[1024];
if( NULL == f )
{
NKDbgPrintfW( L"NULL - Survived %d iterations\r\n", i );
break;
}
}
}
catch( std::bad_alloc& )
{
NKDbgPrintfW( L"std::bad_alloc - Survived %d iterations\r\n", i );
}
return 0;
}
这将打印:NULL - Survived 29599 iterations
。
我没有链接到 nothrownew.obj
,因此我应该期待一个 std::bad_alloc
异常。 http://msdn.microsoft.com/en -us/library/kftdy56f%28v=VS.90%29.aspx
有人知道发生了什么事吗?
谢谢, 保罗·H
I have a Visual Studio 2008 Windows Mobile 6.5.3 ARMV4I project. I check for out of memory conditions using a try/catch block looking for std::bad_alloc
exceptions. But, through testing I've found that it can actually just return a NULL value and not throw an exception.
int _tmain( int argc, _TCHAR* argv[] )
{
int i = 0;
try
{
for( ; i < 30000; ++i )
{
BYTE* f = new BYTE[1024];
if( NULL == f )
{
NKDbgPrintfW( L"NULL - Survived %d iterations\r\n", i );
break;
}
}
}
catch( std::bad_alloc& )
{
NKDbgPrintfW( L"std::bad_alloc - Survived %d iterations\r\n", i );
}
return 0;
}
This prints: NULL - Survived 29599 iterations
.
I am not linking against nothrownew.obj
and according to this I should expect a std::bad_alloc
exception . http://msdn.microsoft.com/en-us/library/kftdy56f%28v=VS.90%29.aspx
Does anybody know what's going on?
Thanks,
PaulH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所观察到的仅仅意味着编译器提供的库已损坏。从某种意义上说,它是不符合C++语言标准的要求的。
new
的这一特殊问题已存在于早期版本的 C++ 标准库(如 VC 6.0 提供的版本)中。后来更新了一些版本的编译器/库以满足标准要求。显然,Windows Mobile 版本保持不变。很可能这是故意这样做的,以保持与旧代码的兼容性。您可能还想检查一些可能控制此行为的编译器配置开关。不知道是否有这样的开关。
What you observe simply means that the library supplied with the compiler is broken. It is broken in a sense that it does not follow the requirements of C++ language standard.
This particular issue with
new
has been present in the earlier versions of C++ standard library (like the one supplied with VC 6.0). Later some versions of the compiler/library were updated to satisfy standard requirements. Apparently, the Windows Mobile version was left unchanged.It is quite possible that it was done intentionally in order to preserve the compatibility with older code. You might also wanna check for some compiler configuration switch that might control this behavior. I don't know whether such a switch exists.
错误样式取决于所使用的新表达式的类型。标准对分配函数是这样说的:
5.3.4/13:
然后在 18.4.1.1/5 中,我们了解到分配函数的
nothrot
版本仅在指示nothrot
的放置样式new
表达式中调用:换句话说,编译器似乎不符合标准。这很可能是为了避免支持移动设备上的异常所需的库代码,并且可能作为 Windows Mobile 编译器文档的一部分进行记录。
The error style depends on the type of new expression used. The standard says this about the allocation function:
5.3.4/13:
Then in 18.4.1.1/5 we learn that the
nothrow
version of the allocation function is called only in a placement stylenew
expression indicatingnothrow
:In other words, the compiler appears to be non-conforming. Most likely this is to avoid library code needed to support exceptions on mobile devices and is probably documented as part of the Windows Mobile compiler documentation.