c++ - 复制构造函数在相同情况下失败并成功

标签 c++ constructor gdb copy

我正在为一个数学项目编写一个矩阵类,我遇到了一个奇怪的问题,即我编写的​​复制构造函数在一个函数中失败但在另一个函数中成功 - 并且情况看似相同。我已经通过 gdb 运行它并执行了复制构造函数,但不知何故在它完成后没有分配值。

这是 gdb 的输出:

(gdb) break main.cpp:20
Breakpoint 1 at 0x8048913: file main.cpp, line 20.
(gdb) run
Starting program: /home/ian/Documents/math471/src/compress 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 
printing
1 1 1 1 
1 2 3 4 
1 3 5 7 
1 4 7 10 

Breakpoint 1, main (argc=1, argv=0xbffff334) at main.cpp:20
20          tridiagonalize(A);
(gdb) s
tridiagonalize (A=...) at tridiagonalize.cpp:10
10          Matrix result(A);
(gdb) print result
$1 = {transposed = false, height = 3903476, width = 0, data = 0x0}
(gdb) s
Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:176
176         height = A.getHeight();
(gdb) print height
$2 = 0
(gdb) n
177         width = A.getWidth();
(gdb) print height
$3 = 4
(gdb) n
178         data = new double*[height];
(gdb) print width
$4 = 4
(gdb) n
179         for(int i = 0; i < height; i++){
(gdb) break matrix.cpp:185
Breakpoint 2 at 0x80492ba: file matrix.cpp, line 185.
(gdb) c
Continuing.

Breakpoint 2, Matrix::Matrix (this=0xbffff23c, A=...) at matrix.cpp:185
185         transposed = false;
(gdb) n
186 }
(gdb) print transposed
$5 = false
(gdb) print this
$6 = (Matrix * const) 0xbffff23c
(gdb) print this->height
$7 = 4
(gdb) n
tridiagonalize (A=...) at tridiagonalize.cpp:11
11          int n = A.getWidth();
(gdb) print result
$8 = {transposed = false, height = 3903476, width = 0, data = 0x0}
(gdb) 

如您所见,结果中的值在构造函数之前和之后是相同的。

#include "matrix.h"
#include "tridiagonalize.h"


using namespace std;

void function(Matrix &A);

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

        Matrix A(4, 4);
        for(int i = 0; i < 16; i++){
                A.set(i/4, i%4, (i%4)*(i/4)+1);
        }
        function(A);
        Matrix B = A;
        A.print();
        B.print();

        tridiagonalize(A);
        //A.print();

        return 0;
}

void function(Matrix &A){
        Matrix result(A);
        A.print();
        result.print();
}

Matrix tridiagonalize(Matrix &A){

        Matrix result(A);
        int n = A.getWidth();
        cout << "width: " << n << endl;
        cout << "width of result: " << result.getWidth() << endl;
            return result;
}

编辑:这里是矩阵代码:

void Matrix::set(int i, int j, double value){
        if(transposed){
                int tmp = i;
                i = j;
                j = tmp;
        }
        if(i < 0 || j < 0 || i >= height || j >= width){
                cout << "trying to set value outside of matrix" << endl;
                return;
        }
        if(height > 0 && width > 0){
                data[i][j] = value;
        }
        return;
}

void Matrix::print() const{
        cout << "printing" << endl;
        if(!transposed){
                for(int i = 0; i < height; i++){
                        for(int j = 0; j < width; j++){
                                cout << data[i][j] << " ";
                        }
                        cout << endl;
                }
        }
        else{
                for(int i = 0; i < width; i++){
                        for(int j = 0; j < height; j++){
                                cout << data[j][i] << " ";
                        }
                        cout << endl;
                }
        }
        return;
}

Matrix::~Matrix(){
        //cout << "deleting Matrix" << endl;
        if(data != NULL){
                for(int i = 0; i < height; i++){
                        //cout << data[i] << endl;
                        delete[] data[i];
                }
                //cout << data << endl;
                delete[] data;
        }
}

double Matrix::get(int i, int j) const {
        if(transposed){
                int tmp = i;
                i = j;
                j = tmp;
        }
        if(data != NULL && i >=0 && j >= 0 && i < height && j < width){
                return data[i][j];
        }
        else{
                cout << "error in get" << endl;
        }
        return -1;
}

int Matrix::getHeight() const {
        if(transposed){
                return width;
        }
        else{
                return height;
        }
}

int Matrix::getWidth() const {
        if(transposed){
                return height;
        }
        else{
                return width;
        }
}



Matrix::Matrix(const Matrix& A){
        height = A.getHeight();
        width = A.getWidth();
        data = new double*[height];
        for(int i = 0; i < height; i++){
                data[i] = new double[width];
                for(int j = 0; j < width; j++){
                        data[i][j] = A.get(i,j);
                }
        }
        transposed = false;
}

Matrix Matrix::operator= (Matrix &B){
        if(data != NULL){
                for(int i = 0; i < height; i++){
                        delete[] data[i];
                }
                delete[] data;
        }
        data = NULL;
        Matrix result(B);
        return result;
}

tridiagonalize 函数和“function”函数的设置相同,但 function() 有效而 tridiagonalize() 无效。知道为什么会这样吗?

编辑:添加了矩阵代码

最佳答案

Matrix::operator=() 无法正常工作,它释放了旧的 data 数组,但没有创建一个包含应有数据的新数组被分配。

它应该看起来像这样:

Matrix& Matrix::operator= (const Matrix &B){
   // Don't do anything if B is the same object
   if (&B == this)
      return *this;

   // delete data, as in the current version
   if(data != NULL){
      ...
   }

   // Create new array of arrays, fill with numbers from B
   data = new double*[B.getHeight()];
   ...

   return *this;
}

关于c++ - 复制构造函数在相同情况下失败并成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8467130/

相关文章:

c++ - Eclipse 无法执行 MI 命令 -target-select remote

linux-kernel - 如何在 GDB 中 'reload' 源文件

C++ []运算符重载问题

c++ - Sprite 如何跳跃以及它在c++中基于什么逻辑?

c++ - 两个语句 C++ 之间的区别

c++ - 初始化一个线程,它是构造函数内部的成员变量

java - 使用泛型处理构造函数中的类型删除

c++ - 堆分配变量的结构成员对齐

c++ - 实时检测图像是否不同

c - 如何处理gdb中的 `__int128`?