我的 C++ 程序(使用矩阵处理一些计算,请参阅下面的文件)崩溃并显示以下消息:
*** 检测到 glibc *** ./matrix: munmap_chunk(): 无效指针: 0x08bfd068 ***
其次是回溯和内存映射。当我第一次在 Matrix 类中调用 set-Method 时会发生这种情况 - 但我不知道我做错了什么......非常感谢每一个帮助(以及改进我的代码的一般提示)!
数组.cpp
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
using namespace std;
Array::Array(){
Array(10);
}
Array::Array(int size){
data = new int[size];
memset(data, 0, sizeof(data));
length = size;
}
Array::~Array(){
delete [] data;
}
void Array::set(int pos, int value){
if(pos < 0) pos = 0;
if(pos >= length) pos = length-1;
*(data + pos) = value;
}
int Array::get(int pos){
if(pos < 0) pos = 0;
if(pos >= length) pos = length-1;
return *(data + pos);
}
void Array::print(){
for(int i = 0; i < length; i++){
cout << *(data + i) << "\t";
}
cout << endl;
return;
}
/*works only for arrays of length 9*/
int Array::find_max(int data[]){
int max = data[0];
for(int i = 1; i < 9; i++){
if(data[i] > max) max = data[i];
}
return max;
}
矩阵.h
#ifndef MATRIX_H
#define MATRIX_H
#include "Array.h"
class Matrix{
private:
Array * data;
int height;
int width;
public:
Matrix();
...
};
#endif
矩阵.cpp
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(){
Matrix(10, 10);
}
Matrix::Matrix(int h, int w){
height = h;
width = w;
data = new Array(height);
for(int i = 0; i < height; i++){
*(data + i) = *(new Array(width));
}
}
Matrix::~Matrix(){
for(int i = 0; i < height; i++){
Array * row = (data + i);
delete row;
}
delete data;
}
void Matrix::set(int h, int w, int value){
Array row = *(data + h);
row.set(w, value);
}
...
主文件
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
#include "Matrix.h"
using namespace std;
int main(int argc, char** argv){
if(argc != 3){
cout << "usage: " << argv[0] << " <m> x <n>" << endl;
exit(-1);
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
Matrix * myMatrix = new Matrix(m, n);
/*fill matrix randomly*/
int guess, minus;
srand(time(NULL));
for(int r = 0; r < m; r++){
for(int c = 0; c < n; c++){
guess = rand() % 1001;
minus = rand() % 2;
if(minus == 0) guess *= -1;
std::cout << " set " << c << ", " << r << " " << guess << std::endl;
myMatrix->set(r, c, guess);
}
}
...
delete myMatrix;
...
return 0;
}