简体   繁体   English

为什么在抛出'std :: bad_alloc'的实例后终止调用?

[英]why terminate called after throwing an instance of 'std::bad_alloc'?

Every 1 second, function works. 每1秒,功能起作用。
my system the linux. 我的系统是linux。 Runs suddenly dies. 奔跑突然死了。

    -----global-------
    static int arrayNum[33000];
    -------------------
function(){
    unsigned short int**  US_INT;
    US_INT= new unsigned short int*[255];
            for(int i = 0; i < 255; i++)
            {
                US_INT[i] = new unsigned short int[128];
                memset(US_INT[i], 0, sizeof(unsigned short int) * 128);
            }
    double x;
    double y;
    int cnt= 0;  
            int nArrayCount=0;
          for(int i = 0; i < 255; i++)
            {

                for(int j=0;j<128;j++){

                    x=j;
    y=cnt
                    nArray[nArrayCount]=US_INT[i][j];

                    nArrayCount++;

                }
                cnt=cnt+(256/255); 

            }


     for(int i = 0; i < 255; i++)
            {
                delete US_INT[i];
            }

            delete[] US_INT;
}

program stop. 程序停止。 and message↓ terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 抛出'std :: bad_alloc'的实例后调用的消息↓终止what():std :: bad_alloc

The bad_alloc exception is triggered by a failure in the memory allocation (so one of your new ). bad_alloc异常是由内存分配失败触发的(所以你的一个是new )。 terminate() is called automatically because you don't catch this exception. 自动调用terminate()因为您没有捕获此异常。

The root cause of the bad_alloc is that you don't have enough memory (or the free store is corrupted). bad_alloc的根本原因是您没有足够的内存(或者免费存储已损坏)。 This could for example happen if you reapeatedly fail to free memory in some loops. 例如,如果你在某些循环中没有释放内存,那么就会发生这种情况。

In fact, in your code, it appears that you don't delete correctly the arrays US_INT[i] . 实际上,在您的代码中,您似乎没有正确删除数组US_INT[i] Your must use delete[]US_INT[i] . 你必须使用delete[]US_INT[i] As a general rule , every time you use new[] , you shall use delete[] . 作为一般规则 ,每次使用new[] ,都应使用delete[]

PS: You could also opt for vectors instead of arrays and free your mind from memory maangement issues. PS: 你也可以选择向量而不是数组,让你的思想从内存管理问题中释放出来。

暂无
暂无

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

相关问题 抛出&#39;std :: bad_alloc&#39;实例后调用Mergemap终止 - Mergemap terminate called after throwing an instance of 'std::bad_alloc' 收到错误“在抛出&#39;std::bad_alloc&#39;what() 的实例后调用终止:std::bad_alloc” - getting error "terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" 在抛出'std :: bad_alloc'的实例后调用终止what():std :: bad_alloc中止 - terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted 在抛出“ std :: bad_alloc”实例的what():std :: bad_alloc实例后终止调用 - terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" 在抛出&#39;std::bad _alloc&#39; what(): std::bad_alloc in c++ 实例后调用终止 - Terminate called after throwing an instance of 'std::bad _alloc' what(): std::bad_alloc in c++ 在抛出 'std::bad_alloc' 的实例后调用终止 what(): std::bad_alloc 在推回向量后 - terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc after pushing back into a vector Node.js错误“在抛出&#39;std :: bad_alloc&#39;的实例后调用终止what():std :: bad_alloc” - Node.js error “terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc” 在运行我的代码时显示在抛出 'std::bad_alloc' what() 实例后调用终止:std::bad_alloc - On running my code is showing terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc C++ 错误:在抛出 'std::bad_alloc' what(): std::bad_alloc 的实例后调用终止 - C++ error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc 如何修复 C++ 错误:在抛出“std::bad_alloc”实例后调用终止。 什么():std::bad_alloc - How to fix C++ error: terminate called after throwing an instance of 'std::bad_alloc'. what(): std::bad_alloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM