gpt4 book ai didi

c++ - 使用二维指针数组时出现段错误

转载 作者:行者123 更新时间:2023-11-28 01:07:37 27 4
gpt4 key购买 nike

嘿,这对我来说真的是一个棘手的问题。

ma​​in.cpp

#include <stdlib.h>
#include <iostream>
#include "Matrix.h"

int main(int argc, char** argv) {
// Dummy matrix
double row1[3] = {3, -1, -2};
double col1[3] = {4, 3, 1};
// Initialize matrix with 4 x 2 dimensions
Matrix<double> *m = new Matrix<double>(1, 3);
Matrix<double> *n = new Matrix<double>(3, 1);
Matrix<double> *mn;
// Set each row or column
m->set_row(row1, 0);
n->set_col(col1, 0);
std::cout << "Matrix M: \n";
m->print();
std::cout << "Matrix N: \n";
n->print();
std::cout << "Matrix MN: \n";
mn = m->mult(n);
mn->print();
return (EXIT_SUCCESS);
}

矩阵.h

/* 
* File: Matrix.h
* Author: charles
*
* Created on March 16, 2011, 12:45 AM
*/
#ifndef _MATRIX_H_
#define _MATRIX_H_

template <typename T>
class Matrix {
public:

Matrix(int _r, int _c){
// Set row and column size, then initialize matrix
_rows = _r;
_cols = _c;
_matrix = new T * [_rows];
for(int i = 0; i < _rows; ++i){
_matrix[i] = new T [_cols];
}
}

virtual ~Matrix(){
// Delete everything
for(int i = 0; i < _cols; ++i){
delete[] _matrix[i];
}
delete[] _matrix;
}

// Get number of rows
unsigned int get_rows(){
return _rows;
}

// Get number of columns
unsigned int get_cols(){
return _cols;
}

// Returns the row from _matrix as an array
T* get_row(int _r){
T* _t = new T [_cols];
for(int i = 0; i < _cols; ++i){
_t[i] = _matrix[_r][i];
}
return _t;
}

// Returns the column from _matrix as an array
T* get_col(int _c){
T* _t = new T [_rows];
for(int i = 0; i < _rows; ++i){
_t[i] = _matrix[i][_c];
}
return _t;
}

T get_elem(int _r, int _c){
return _matrix[_r][_c];
}

// Set a specific row with an array of type T
void set_row( T _t[], int _r ){
for(int i = 0; i < _cols; ++i){
_matrix[_r][i] = _t[i];
}
}

// Set a specific column with an array of type T
void set_col( T _t[], int _c){
for(int i = 0; i < _rows; ++i){
_matrix[i][_c] = _t[i];
}
}

// Set a specific index in the matrix with value T
void set_elem(T _t, int _r, int _c){
_matrix[_r][_c] = _t;
}

// Test to see if matrix is square
bool is_square(){
return (_rows == _cols);
}

// Print contents of matrix for debugging purposes
void print(){
for(int i = 0; i < _rows; ++i){
for(int j = 0; j < _cols; ++j){
std::cout << _matrix[i][j] << " ";
}
std::cout << "\n";
}
}

T comp_mult(int _r, T* _c){
T temp;
for(int i = 0; i < _cols; ++i){
std::cout << "Comp " << i << "\n";
if(i == 0) temp = _matrix[_r][i] * _c[i];
else temp += _matrix[_r][i] * _c[i];
}
return temp;
}

// Add one matrix to another and return new matrix
Matrix* add(Matrix* _m){

// Cannot add matrices if they do not have the same dimensions
if(!(_rows == _m->get_rows() && _cols == _m->get_cols())){
std::cout << "Not equal!";
return NULL;
}
else{
Matrix<T>* _t = new Matrix<T>(_rows, _cols);
for(int i = 0; i < _rows; ++i){
for(int j = 0; j < _cols; ++j){
_t->set_elem(_matrix[i][j] + _m->get_elem(i, j), i, j);
}
}
return _t;
}
}

// Multiply two matrices and return new matrix
Matrix* mult(Matrix* _m){
// If dimensions are not compatible return NULL
if(_cols != _m->get_rows()){
return NULL;
}
else{
Matrix<T>* _t = new Matrix<T>(_rows, _m->get_cols());
// Print out dimensions
// std::cout << "Dimensions: r = " << _t->get_rows() << " c = " << _t->get_cols() << "\n";
// Each row of _t
for(int i = 0; i < _t->get_rows(); ++i){
// Each value in each row of _t
for(int j = 0; j < _t->get_cols(); ++j){
T temp; // Temp variable to hold value for _t[i][j]
// Each row in _matrix
std::cout << "Loop i:" << i << "\n";
temp = this->comp_mult(i, _m->get_col(j));
std::cout << "loop j: " << j << "\n";
std::cout << "TEMP = " << temp << "\n";
_t->set_elem(temp, i, j); // this is where i segfault
}
}
}
}

// Multiply entire matrix by number and return new matrix
Matrix* scalar(T _n){
Matrix<T>* _t = new Matrix<T>(_rows, _cols);
for(int i = 0; i < _rows; ++i){
for(int j = 0; j < _cols; ++j){
_t->set_elem(_matrix[i][j] * _n, i, j);
}
}
return _t;
}

private:
unsigned int _rows; // Number of rows
unsigned int _cols; // Number of columns
T** _matrix; // Actual matrix data

};

#endif /* _MATRIX_H_ */

示例输出

Matrix M: 
3 -1 -2
Matrix N:
4
3
1
Matrix MN:
Loop i:0
Comp 0
Comp 1
Comp 2
loop j: 0
TEMP = 7
Segmentation fault

所以我遇到的问题是在尝试为新矩阵分配值时出现段错误。这对我来说没有意义,因为我已经初始化了我试图赋值的新矩阵,我知道它的大小,我不想访问矩阵外的内存,而且我知道我的临时变量有一个值.有人有建议吗?

最佳答案

您忘记从 mult() 返回 _t - 调试器可能会为您提供调用 mn->print( 时发生的崩溃的错误位置)

此外,get_colget_row 会泄漏内存,您需要创建一个复制构造函数和一个赋值运算符。

关于c++ - 使用二维指针数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com