Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;错误:在抛出';的实例后调用terminate;std::bad#u alloc';_C++_Memory Management_Runtime Error - Fatal编程技术网

C++ C++;错误:在抛出';的实例后调用terminate;std::bad#u alloc';

C++ C++;错误:在抛出';的实例后调用terminate;std::bad#u alloc';,c++,memory-management,runtime-error,C++,Memory Management,Runtime Error,我在eclipse上使用了下面的代码,并且在抛出'std::bad_alloc'what():std::bad_alloc'实例后调用了“terminate”错误 我有发票类和发票类 class Invoice { public: //...... other functions..... private: string name; Mat im; int width; int height; vector<RectInvoice*> rect

我在eclipse上使用了下面的代码,并且在抛出'std::bad_alloc'what():std::bad_alloc'实例后调用了“terminate”错误

我有发票类和发票类

class Invoice {
public:

    //...... other functions.....
private:
   string name;
   Mat im;
   int width;
   int height;
   vector<RectInvoice*> rectInvoiceVector; 
};
类别发票{
公众:
//……其他功能。。。。。
私人:
字符串名;
Mat im;
整数宽度;
内部高度;
向量向量;
};
我在发票的方法上使用下面的代码

        // vect : vector<int> *vect;

        RectInvoice rect(vect,im,x, y, w ,h);
        this->rectInvoiceVector.push_back(&rect);
//vect:vector*vect;
rect(向量,im,x,y,w,h);
这->rectInvoiceVector.push_back(&rect);

我想更改eclipse.ini文件中的eclipse内存。但是我没有授权这样做。我怎么做呢?

某个东西抛出类型为
std::bad\u alloc
的异常,表明内存不足。此异常会一直传播到
main
,在那里它会“脱落”到您的程序并导致您看到的错误消息


因为这里没有人知道什么是“RectInvoice”、“rectInvoiceVector”、“vect”、“im”等等,所以我们无法告诉您到底是什么导致了内存不足的情况。您甚至没有发布真正的代码,因为
w h
看起来像是语法错误。

代码中的问题是无法将局部变量(例如函数的局部变量)的内存地址存储在全局变量中:

RectInvoice rect(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(&rect);
RectInvoice *rect =  new RectInvoice(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(rect);
其中,
&rect
是一个临时地址(存储在函数的激活注册表中),该地址将在函数结束时被销毁

代码应创建一个动态变量:

RectInvoice rect(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(&rect);
RectInvoice *rect =  new RectInvoice(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(rect);
在这里,您使用的堆地址在函数执行结束时不会被销毁。 告诉我它是否对你有用


干杯

我的项目有很多文件,cpp和.h。所以我放了一小部分。我想我应该更改.ini文件。我说得对吗?好的。。谢谢你的推荐。我是这个平台上的新手。我正在学习:)这个问题对我也很有帮助。如果不再需要,请不要忘记删除每个元素。如果使用动态内存分配,则应使用智能指针(例如std::unique_ptr),以确保最终删除分配的所有内容。