【问题标题】:Getting std::bad_alloc after some time一段时间后得到 std::bad_alloc
【发布时间】:2015-03-06 03:54:52
【问题描述】:

我有这段代码在 5-fold-crossvalidaiton 上运行,代码在每个折叠上都做同样的事情,但问题是;它消耗了每次折叠的内存交换,并且似乎它没有释放内存,你能指出问题出在哪里吗? 我得到的错误是:

在抛出 'std::bad_alloc' 实例后调用终止
what(): std::bad_alloc ./run.sh: line 3: 12014 Aborted
(核心转储)./my-boost

代码是:

#include <iostream>
#include <boost/dynamic_bitset.hpp>       
#include <vector>
#include <fstream>


using namespace::std;

struct point {
    float fpr;
    float tpr;
    point(float x, float y)
    {
        fpr = x;
        tpr = y;
    }
};

float** combine_crisp(boost::dynamic_bitset<unsigned char> label, boost::dynamic_bitset<unsigned char> r_1, boost::dynamic_bitset<unsigned char> r_2, vector<int> fun)
{
    int  k = 0;
    boost::dynamic_bitset<unsigned char> r_12;
    const int LENGTH =   (int) fun.size();
    float **FprTpr;
    FprTpr = new float*[LENGTH];

    int P = (int) label.count();
    int N = (int) (~label).count();
    boost::dynamic_bitset<unsigned char> notlabel(~label);
    for(vector<int>::iterator it = fun.begin(); it != fun.end(); ++it)
    {
        FprTpr[k] = new float[2];
        if (*it == 1)         //----------------> 'A AND B'
        {
            r_12 = r_1 & r_2;
        }
        else if(*it == 2)  //---------------> 'NOT A AND B'
        {
            r_12 = ~r_1 & r_2;
        }
        else if(*it == 3) //----------------> 'A AND NOT B'
        {
            r_12 = r_1 & ~r_2;
        }
        else if(*it == 4) //----------------> 'A NAND B'
        {
            r_12 = ~(r_1 & r_2);
        }
        else if(*it == 5) //----------------> 'A OR B'
        {
            r_12 = r_1 | r_2;
        }
        else if(*it == 6) //----------------> 'NOT A OR B'; 'A IMP B'
        {
            r_12 = ~r_1 | r_2;
        }
        else if(*it == 7) //----------------> 'A OR NOT B' ;'B IMP A'
        {
            r_12 = r_1 | ~r_2;
        }
        else if(*it == 8)  //----------------> 'A NOR B'
        {
            r_12 = ~(r_1 | r_2);
        }
        else if(*it == 9) //----------------> 'A XOR B'
        {
            r_12 = r_1 ^ r_2;
        }
        else if(*it == 10) //----------------> 'A EQV B'
        {
            r_12 = ~(r_1 ^ r_2);
        }
        FprTpr[k][0] = (r_12 & notlabel).count() / (float)N;
        FprTpr[k][1] = (r_12 & label).count() / (float)P;

        k++;
    }
    return FprTpr;
}

int main(int argc, char* argv[])
{               

    std::string inputFile;
    std::string outputFile;
    int  first_classifier  = 0;      

    for (int fo = 1; fo <= 5; fo++)
    {

        inputFile = "./vectors.txt";    

        outputFile += "./bccpoints.txt";

        std::ifstream infileFirst(inputFile);

        boost::dynamic_bitset<unsigned char> label;

        std::vector<boost::dynamic_bitset<unsigned char> > classifiers;

        std::string line;
        int numberOfClassifiers = -1;
        int lenOfClassifiers = -1;
        while (std::getline(infileFirst, line))
        {
            if (numberOfClassifiers == -1)
            {
                lenOfClassifiers = (int)std::string(line).length();
                label = boost::dynamic_bitset<unsigned char> (line);
            }
            else
            {
                classifiers.push_back(boost::dynamic_bitset<unsigned char> (line));
            }
            numberOfClassifiers++;
        }

        static const int arr[] = {1,2,3,4, 5,6,7,8,9,10};
        vector<int> fun (arr, arr + sizeof(arr) / sizeof(arr[0]) );
        static const int BOOLEANSIZE = fun.size();

        int NUMBER = numberOfClassifiers;

        float **rs_tmp;
        vector<point> current_points;

        for (int i = first_classifier; i < NUMBER; i++)
        {
            for (int j = 0; j < NUMBER; j++)
            {
                rs_tmp = combine_crisp(label, classifiers[i], classifiers[j], fun);

                for (int kk = 0; kk < BOOLEANSIZE; kk++) //creating row
                {
                    current_points.push_back( point(rs_tmp[kk][0], rs_tmp[kk][1] ) );
                    //                current_points.push_back ({rs_tmp[kk][0], rs_tmp[kk][1]});
                }
            }
        }
        delete[] rs_tmp;


        ofstream files;
        files.open (outputFile);
            files.write(reinterpret_cast<char*>(current_points.data()), current_points.size() * sizeof(point));



            std::vector<boost::dynamic_bitset<unsigned char> >().swap(classifiers);
            std::vector<point>().swap(current_points);
    }

}

【问题讨论】:

  • delete[] rs_tmp 这不足以释放您在combine_crisp 函数中创建的内存。在调用 deleteing rs_tmp 之前,您应该遍历函数中的每个 float*newed 以及 delete 它们。这需要在您调用 combine_crisp 函数的同一范围内完成,而不是在所有 for 循环之外
  • 不要使用 new 和 delete,而是使用 std::vector&lt;std::array&lt;int,2&gt;&gt;。您在其他地方使用矢量,因此在这里也使用它是有意义的。如果您没有 C++11 访问权限,则创建一个 2 个整数的结构以存储在向量中。

标签: c++ c++11 boost


【解决方案1】:

请在下面的代码中找到删除调用,

    for (int i = first_classifier; i < NUMBER; i++)
    {
        for (int j = 0; j < NUMBER; j++)
        {
            rs_tmp = combine_crisp(label, classifiers[i], classifiers[j], fun);

            for (int kk = 0; kk < BOOLEANSIZE; kk++) //creating row
            {
                current_points.push_back( point(rs_tmp[kk][0], rs_tmp[kk][1] ) );
                //                current_points.push_back ({rs_tmp[kk][0], rs_tmp[kk][1]});
            }

            int size = fun.size();
            for(int i = 0; i < size; ++i)     // Iterate over each item in the array
            {
                delete[] rs_tmp[i]; // free the float array of size 2 you created in the other function
            }

            delete[] rs_tmp; // finally delete the float* array of size - fun.size()
        }
    } 

【讨论】:

  • 我以前这样做过,但它给了我分段错误错误。我想因为我没有新的东西我不能做 delete[] rs_tmp[i];
  • @Am1rr3zA - 它不应该在combine_crisp 函数中分配一个长度为fun.size() 的动态数组,它可以保存指向float 的指针,然后你新建一个大小为float 的数组2 并分配给第一个数组中的每个元素......所以如果在正确的位置进行,这不应该给你分段错误..
  • @Am1rr3zA 能否请您进行我所做的更改,看看是否有效?
  • @Am1rr3zA 你做了new 的事情,在combine_crisp 里面。
【解决方案2】:

delete[] rs_tmp 只是删除指向float 变量的指针,而不是指向float 的指针。你的很多内存仍然被分配,最后抛出std::bad_alloc。因此,您需要正确释放分配的内存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多