简体   繁体   English

我期望它不会抛出Bad_alloc

[英]Bad_alloc not thrown when I expect it to

Consider this simple program: 考虑这个简单的程序:

#include <exception>
#include <iostream>

int main(void)
{
    const std::size_t size = 1<<31;
    int *a = NULL;

    try
    {
        a = new int[size];
    }
    catch (std::exception &e)
    {
        std::cerr << "caught some bad guy" << std::endl;
        return 1;
    }

    if (a == NULL)
    {
        std::cerr << "it's null, can't touch this" << std::endl;
        return 1;
    }

    std::cerr << "looks like 'a' is allocated alright!" << std::endl;

    for (size_t i = 0; i < size; i ++)
        std::cout << a[i] << " ";

    return 0;
}

Commentary 评论

  • I try to allocate some ridiculous amount of memory: (1<<31) * sizeof(int) == 8GB 我尝试分配一些荒谬的内存: (1<<31) * sizeof(int) == 8GB
  • I add safety checks 我加上安全检查
    • Catching std::exception , which should catch std::bad_alloc among other exceptions... 捕获std::exception ,它应该在其他异常中捕获std::bad_alloc ...
    • Check if it's not null (even though for this check to actually make sense, I'd need a = new (std::nothrow) int[size] - but regardless of how I allocate memory, it doesn't work) 检查它是否不为空(即使这个检查实际上有意义,我需要a = new (std::nothrow) int[size] - 但不管我如何分配内存,它都不起作用)

Environment 环境

  • RAM installed: 2GB 安装RAM:2GB
  • Operating system: Debian 操作系统:Debian
  • Architecture: 32-bit 架构:32位

Problem 问题

The problem is that the program, instead of early exit, does something like this: 问题是程序,而不是提前退出,做这样的事情:

rr-@burza:~$ g++ test.cpp -o test && ./test
looks like 'a' is allocated alright!
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(...many other zeros here...)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Segmentation fault

The number of zeros printed is exactly 33790, which tells me exactly... nothing. 打印的零数正好是33790,这正好告诉我......什么都没有。 How can I make my program segfault-proof? 如何使我的程序具有段错误?

This seems to be a bug in your environment, which causes integer overflow in implementation of new[] . 这似乎是您环境中的一个错误,它会导致new[]实现中出现整数溢出。 In effect, you are allocating 0 bytes. 实际上,您正在分配0个字节。 It might be this bug . 可能是这个错误 C++03 standard is not clear about what should happen, in C++11 std::bad_array_new_length should be thrown. C ++ 03标准不清楚应该发生什么,在C ++ 11中应该抛出std::bad_array_new_length

If you need to support this system you can check if there is chance for overflow before allocating, for example: 如果您需要支持此系统,您可以在分配之前检查是否有溢出的可能性,例如:

size_t size_t_max = -1;
if (size > size_t_max / sizeof(int))
    throw ...;

This bug might still affect you however if libraries you use don't have such checks (for example implementation of std::vector ). 如果您使用的库没有这样的检查(例如std::vector实现),则此错误可能仍然会影响您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM