一段时间后获取std :: bad_alloc

Am1rr3zA

我有这段代码运行在5折交叉验证中,这些代码在每一折上都做同样的事情,但是问题是;它每次折叠都消耗可交换的内存,并且似乎没有释放内存,您能指出问题出在哪里吗?我得到的错误是:

抛出'std :: bad_alloc'实例之后调用终止方法
what():std :: bad_alloc ./run.sh:第3行:12014已中止
(核心转储)./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);
    }

}
卡尔提克·卡里亚纳·孙达兰

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

    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()
        }
    } 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

奇怪的std :: bad_alloc

来自分类Dev

C ++奇怪的std :: bad_alloc异常

来自分类Dev

C ++ std :: bad_alloc错误

来自分类Dev

C ++ std :: bad_alloc错误

来自分类Dev

C ++向量std :: bad_alloc错误

来自分类Dev

如何修复“std::bad_alloc”

来自分类Dev

在while循环中获取bad_alloc

来自分类Dev

错误更改/ usr /权限后的'std :: bad_alloc'

来自分类Dev

Node.js错误“抛出'std :: bad_alloc'what():std :: bad_alloc实例后调用终止”

来自分类Dev

已经创建的向量中的奇怪std :: bad_alloc

来自分类Dev

如果我得到std :: bad_alloc怎么办?

来自分类Dev

大数据集的dijkstra计算期间的std :: bad_alloc

来自分类Dev

如果我得到std :: bad_alloc怎么办?

来自分类Dev

'new'导致std :: bad_alloc在相对不大的分配上

来自分类Dev

级联两个向量时的std :: bad_alloc

来自分类Dev

程序中的 C++ 向量 std::bad_alloc 错误

来自分类Dev

CPP - 未处理的异常 std::bad_alloc

来自分类Dev

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

来自分类Dev

抛出'std::bad_alloc'的实例后调用C++终止

来自分类Dev

另一个std :: bad_alloc在内存位置

来自分类Dev

将opencv :: Mat转换为std :: string会得到std :: bad_alloc

来自分类Dev

当分配超出限制的对象时,Clang无法抛出std :: bad_alloc

来自分类Dev

std :: bad_alloc在内存位置0x002b123c

来自分类Dev

异常:内存位置0x00e4df90上的std :: bad_alloc

来自分类Dev

std :: bad_alloc在内存位置0x002b123c

来自分类Dev

带有QVector的bad_alloc但不带有std :: vector

来自分类Dev

当分配超出限制的对象时,Clang无法引发std :: bad_alloc

来自分类Dev

为什么返回vector <string>会抛出std :: bad_alloc异常?

来自分类Dev

std :: bad_alloc导致线程成员函数和mex文件崩溃

Related 相关文章

热门标签

归档