Windows Mobile 在“new”失败时返回 0

发布于 2024-11-04 23:43:07 字数 973 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-11-11 23:43:07

您所观察到的仅仅意味着编译器提供的库已损坏。从某种意义上说,它是不符合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.

掩耳倾听 2024-11-11 23:43:07

错误样式取决于所使用的新表达式的类型。标准对分配函数是这样说的:

5.3.4/13:

[注意:除非分配函数
声明为空
异常规范(15.4),
throw(),表示失败
通过抛出分配存储
bad_alloc 异常(第 15 条,
18.4.2.1);否则返回一个非空指针。如果分配
函数声明为空
异常规范, throw(), it
返回 null 表示失败
分配存储空间和非空指针
否则。 ]

然后在 18.4.1.1/5 中,我们了解到分配函数的 nothrot 版本仅在指示 nothrot 的放置样式 new 表达式中调用:

效果:与上面相同,只是它
由 a 的放置版本调用
C++ 程序时的新表达式
更喜欢空指针结果作为
错误指示,而不是
bad_alloc 异常。

换句话说,编译器似乎不符合标准。这很可能是为了避免支持移动设备上的异常所需的库代码,并且可能作为 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:

[Note: unless an allocation function
is declared with an empty
exception specification (15.4),
throw(), it indicates failure to
allocate storage by throwing a
bad_alloc exception (clause 15,
18.4.2.1); it returns a nonnull pointer otherwise. If the allocation
function is declared with an empty
exception specification, throw(), it
returns null to indicate failure to
allocate storage and a nonnull pointer
otherwise. ]

Then in 18.4.1.1/5 we learn that the nothrow version of the allocation function is called only in a placement style new expression indicating nothrow:

Effects: Same as above, except that it
is called by a placement version of a
new expression when a C++ program
prefers a null pointer result as an
error indication, instead of a
bad_alloc exception.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文