【问题标题】:What's wrong with my destructor?我的析构函数有什么问题?
【发布时间】:2011-02-22 16:42:33
【问题描述】:
// Sparse Array Assignment.cpp : Defines the entry point for the console application.  
//  

#include "stdafx.h"  
#include<iostream>  
using namespace std;  
struct node{  
    int row;  
    int col;  
    int value;  
    node* next_in_row;  
    node* next_in_col;  
};  
class MultiLinkedListSparseArray {  
private:  
    char *logfile;  
    node** rowPtr;  
    node** colPtr; // used in constructor  
    node* find_node(node* out);  
    node* ins_node(node* ins,int col);  
    node* in_node(node* ins,node* z);  
    node* get(node* in,int row,int col);  
    bool exist(node* so,int row,int col);  
    node* dummy;  
    int rowd,cold;  
    //add anything you need  
public:  
    MultiLinkedListSparseArray(int rows, int cols);  
    ~MultiLinkedListSparseArray();  
    void setCell(int row, int col, int value);  
    int getCell(int row, int col);  
    void display();  
    void log(char *s);  
    void dump();  
};  
MultiLinkedListSparseArray::MultiLinkedListSparseArray(int rows,int cols){  
    rowPtr=new node* [rows+1];  
    colPtr=new node* [cols+1];  
    for(int n=0;n<=rows;n++)  
        rowPtr[n]=NULL;  
    for(int i=0;i<=cols;i++)  
        colPtr[i]=NULL;  
    rowd=rows;cold=cols;  
}  
MultiLinkedListSparseArray::~MultiLinkedListSparseArray(){  
    cout<<"array is deleted"<<endl;  
    for(int i=rowd;i>=0;i--){  
        for(int j=cold;j>=0;j--){  
            if(exist(rowPtr[i],i,j))  
                delete get(rowPtr[i],i,j);  
        }  
    }              // it stops in the last loop & doesnt show the done word
    cout<<"done"<<endl;  
    delete [] rowPtr;  
    delete [] colPtr;  
    delete dummy;  
}  
void MultiLinkedListSparseArray::log(char *s){  
    logfile=s;  
}  
void MultiLinkedListSparseArray::setCell(int row,int col,int value){  
    if(exist(rowPtr[row],row,col)){  
        (*get(rowPtr[row],row,col)).value=value;  
    }  
    else{  
        if(rowPtr[row]==NULL){  
            rowPtr[row]=new node;  
            (*rowPtr[row]).value=value;  
            (*rowPtr[row]).row=row;  
            (*rowPtr[row]).col=col;  
            (*rowPtr[row]).next_in_row=NULL;  
            (*rowPtr[row]).next_in_col=NULL;  
        }  
        else if((*find_node(rowPtr[row])).col<col){  
            node* out;  
            out=find_node(rowPtr[row]);  
            (*out).next_in_row=new node;  
            (*((*out).next_in_row)).col=col;  
            (*((*out).next_in_row)).row=row;  
            (*((*out).next_in_row)).value=value;  
            (*((*out).next_in_row)).next_in_row=NULL;  
        }  
        else if((*find_node(rowPtr[row])).col>col){  
            node* ins;  
            ins=in_node(rowPtr[row],ins_node(rowPtr[row],col));  
            node* g=(*ins).next_in_row;  
            (*ins).next_in_row=new node;  
            (*((*ins).next_in_row)).col=col;  
            (*(*ins).next_in_row).row=row;  
            (*(*ins).next_in_row).value=value;  
            (*(*ins).next_in_row).next_in_row=g;  
        }  
    }  
}  
int MultiLinkedListSparseArray::getCell(int row,int col){  
        return (*get(rowPtr[row],row,col)).value;  

}  
void MultiLinkedListSparseArray::display(){  
    for(int i=1;i<=5;i++){  
        for(int j=1;j<=5;j++){  
            if(exist(rowPtr[i],i,j))  
                cout<<(*get(rowPtr[i],i,j)).value<<" ";  
            else cout<<"0"<<" ";  
        }  
        cout<<endl;  
    }  
}  
node* MultiLinkedListSparseArray::find_node(node* out)  
{  
    while((*out).next_in_row!=NULL)  
        out=(*out).next_in_row;  
    return out;  
}  
node* MultiLinkedListSparseArray::ins_node(node* ins,int col){  
    while(!((*ins).col>col))  
        ins=(*ins).next_in_row;  
    return ins;  
}  
node* MultiLinkedListSparseArray::in_node(node* ins,node* z){  
    while((*ins).next_in_row!=z)  
        ins=(*ins).next_in_col;  
    return ins;  
}  
node* MultiLinkedListSparseArray::get(node* in,int row,int col){  
    dummy=new node;  
    dummy->value=0;  
    while((*in).col!=col){  
        if((*in).next_in_row==NULL){  
            return dummy;  
        }  
        in=(*in).next_in_row;  
    }  
    return in;  
}  
bool MultiLinkedListSparseArray::exist(node* so,int row,int col){  
    if(so==NULL)  
        return false;  
    else{  
    while((*so).col!=col){  
        if((*so).next_in_row==NULL)  
            return false;  
        else  
            so=(*so).next_in_row;  
    }  
    return true;  
    }  
}    
int _tmain(int argc, _TCHAR* argv[])
{
    MultiLinkedListSparseArray a(5, 5);
a.setCell(1, 5, 4);
a.setCell(2, 1, 2);
a.setCell(2, 2, 3);
a.setCell(3, 4, 5);
a.setCell(4, 1, 7);
a.setCell(4, 5, 8);
a.setCell(5, 2, 6);
cout << "X[4, 1] = " << a.getCell(4, 1) << endl;
cout << "X[4, 5] = " << a.getCell(4, 5) << endl;
cout << "X[2, 2] = " << a.getCell(2, 2) << endl;
cout << "X[5, 1] = " << a.getCell(5, 1) << endl;
a.display();
a.setCell(3, 4, 0);
a.setCell(1, 5, 0);
cout<<a.getCell(1,5)<<endl;
a.setCell(2, 2, 0);
a.setCell(5, 2, 0);
a.setCell(4, 5, 0);
//a.setCell(2, 5, 7); // problem WHY????????!!!!!!!!!!!!!!!
a.setCell(5, 3, 8);
a.setCell(2, 3, 5);
a.setCell(2, 5, 3);
a.setCell(2, 1, 0);
a.setCell(4, 2, 4);
a.setCell(4, 2, 2);
a.setCell(4, 2, 0);
a.setCell(4, 1, 0);
a.setCell(2, 3, 0);
a.setCell(2, 5, 0);
a.setCell(5, 3, 0);
a.display();
return 0;
}      

【问题讨论】:

  • 尝试将代码缩进 4 个空格,这至少可以使其具有一定的可读性。接下来,尝试提出一个问题,而不是仅仅复制所有代码并希望包含最好的、好的信息,即现在发生的事情以及当代码正确时您期望发生的事情。
  • a) 下次请正确格式化您的代码。 b) 只是在这里转储这么多代码,然后问“它有什么问题?”是一种不寻求帮助的可靠方法。请对你的问题稍加努力。
  • 稀疏数组赋值.cpp ???在家工作?如果是这样,请标记为...
  • 这是一个“Plz send de codz”问题(恕我直言),因此可以关闭。
  • 我不是在询问它可以正常工作的整个代码,它只是析构函数卡在 for 循环中并且没有继续运行程序 抱歉,代码很长,但我只是想让你看到新的创建指针以查看问题所在。抱歉,我不知道代码缩进这是我的第二个问题

标签: c++ oop pointers list destructor


【解决方案1】:

事情显然出错了,因为get 方法(我认为应该标记为const)进行内存分配。我不明白为什么要这样做,你应该考虑一下。

因此,您的 get 方法每次调用时都会为 dummy 分配内存(这是泄漏)。但是,更糟糕的是,如果您的类的一系列用户操作没有一次或多次调用get,那么您的虚拟对象仍然只是一个未初始化的指针。然后,当你调用你的析构函数时,你会得到一个错误,因为你不能释放垃圾指针指向的内存。

尝试重新思考、重构甚至重写您的代码。

【讨论】:

  • 问题不在于虚拟对象,它甚至在删除虚拟指针之前就停止了,因此问题出在循环中。但谢谢你的回答我会考虑重写我的代码
  • 不确定你的真实代码,但你写了关于析构函数的问题,我的测试代码是MultiLinkedListSparseArray m(25, 25);。而且,是的,尝试删除您的dummy 失败...
  • 但是,在到达虚拟节​​点之前它不会失败???在我的编译器上,它无法删除创建的节点(析构函数内的 for 循环)
  • 见上一条评论。除了创建和破坏之外,我什至没有尝试对您的课程做更有用的事情。但是我很确定,如果在您尝试处理元素时它在循环中失败,那么这完全是关于内存管理的。您可以尝试逐行调试您的代码,并找到代码行为与您期望的不同的地方。
  • 实际上,一切都是为了努力:)
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 2021-04-11
  • 2015-09-30
  • 2017-10-07
  • 2013-07-12
相关资源
最近更新 更多