Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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/3/arrays/13.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/9/opencv/3.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++ 析构函数删除数组时出错 class Celda { 私人: int*bloques_2;; 公众: 塞尔达(国际码); ~Celda(){delete[]bloques_;}; }; 塞尔达::塞尔达(整数大小) { bloques_uques=新整数[大小]; bloq_ocupados_uu0; tam=尺寸; 对于(int i=0;i_C++_Arrays_Delete Operator - Fatal编程技术网

C++ 析构函数删除数组时出错 class Celda { 私人: int*bloques_2;; 公众: 塞尔达(国际码); ~Celda(){delete[]bloques_;}; }; 塞尔达::塞尔达(整数大小) { bloques_uques=新整数[大小]; bloq_ocupados_uu0; tam=尺寸; 对于(int i=0;i

C++ 析构函数删除数组时出错 class Celda { 私人: int*bloques_2;; 公众: 塞尔达(国际码); ~Celda(){delete[]bloques_;}; }; 塞尔达::塞尔达(整数大小) { bloques_uques=新整数[大小]; bloq_ocupados_uu0; tam=尺寸; 对于(int i=0;i,c++,arrays,delete-operator,C++,Arrays,Delete Operator,我现在有两个编码类,Celda和Tabla,当我运行我的程序时,它会放在末尾: *glibc检测到:free():无效指针:0x0000000002391338* 调试器停止删除bloques_uuu指针太多次(比如30次)调用析构函数 我的错误是什么 tabla_i[i]分配了new,但释放了delete[] 换成 class Celda { private: int* bloques_; public: Celda(int size); ~Celda(){ delet

我现在有两个编码类,Celda和Tabla,当我运行我的程序时,它会放在末尾:

*glibc检测到:free():无效指针:0x0000000002391338*

调试器停止删除bloques_uuu指针太多次(比如30次)调用析构函数


我的错误是什么

tabla_i[i]分配了new,但释放了delete[]

换成

class Celda
{
private:
    int* bloques_;
public:
    Celda(int size);
    ~Celda(){ delete [] bloques_; };
};
Celda :: Celda(int size)
{
    bloques_ = new int[size];
    bloq_ocupados_ = 0;
    tam_ = size;
    for(int i = 0 ; i < tam_ ; ++i){
        bloques_[i] = 0;
    }
}

Tabla :: Tabla(int numcel, int tambloq)
{
    nceldas_ = numcel; tam_bloque_ = tambloq;
    tabla_ = new Celda*[nceldas_];
    for(int i = 0 ; i < nceldas_ ; ++i){
        tabla_[i] = new Celda(tam_bloque_);
}
    ocupadas_ = 0;
}

Tabla :: ~Tabla()
{
    for(int i = 0 ; i < nceldas_ ; ++i){
        delete [] tabla_[i];
    }
    delete [] tabla_;
}

而且它会起作用。

代码中没有copy-ctor和copy-assignment,但所有类似乎都拥有自己的资源。这可能是一个错误,如果你曾经分配或复制构造的东西,可以是你的无效免费的原因。没有更多的代码无法回答。我无法复制更多的代码,因为我无法编辑和粘贴更多。。。但是[]删除是失败的,我想……是的,当然是。但是您需要找出这些指针哪里出错了,这可能是在类的复制操作中,并导致双自由。我建议扔掉这些代码,改用
std::vector
 delete tabla_[i];