My C++ program (handling some calculation with matrices, see files below) crashes with the following messages:
*** glibc detected *** ./matrix: munmap_chunk(): invalid pointer: 0x08bfd068 ***
followed by the backtrace and memory map. It happens when I call the set-Method in the Matrix-class for the first time - yet I don't know what I'm doing wrong ... Every help (and general hints for improvement of my code) very appreciated!
Array.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;
}
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include "Array.h"
class Matrix{
    private:
        Array * data;
        int height;
        int width;
    public:
        Matrix();
        ...
};
#endif
Matrix.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);
}
...
main.cpp
#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;
}
 
     
    