Matrix printing

Feb 18, 2013 at 9:33pm
I need help getting my initializer to work for my project. I get a segmentation fault when I try to print out what I've done. If I could receive any help, I would greatly appreciate it.

#include <iostream>

struct matrix {

protected:
double * * data;
int rows, columns, size;

public:
matrix(int row, int column) {
size = row * column;
columns = column;
rows = row;
data = new double * [size];
}

double * get(int row, int column);
void set(int row, int column, double value);
void resize(int row, int column);
matrix clone();
void print();
void init(double val);
};

.
.
.

void matrix::init(double val) {
for(int i=0; i<rows; i += 1){
for(int i=0; i<columns; i += 1) {
* data[i] = 0.0;
}
}
}
(if you were linked here by my last post, this is what I needed help with before:

matrix matrix::clone() {
matrix newmat(rows, columns);
for( int i=0; i<size; i += 1) {
newmat.set((rows+1), columns, * data[i]);
}
return newmat;
}
)

Thanks


Feb 18, 2013 at 9:45pm
1
2
3
4
5
6
    matrix(int row, int column) {
        size = row * column;
        columns = column;
        rows = row;
        data = new double * [size];
    }


Here you allocate memory for size pointers to double. Those pointers, however, point to random places in memory that you do not own, so using those uninitialized pointers results in undefined behavior.
Feb 18, 2013 at 10:47pm
I get
lab2b.cpp:14: error: cannot convert `double*' to `double**' in assignment
when I try to do that though.
Feb 18, 2013 at 10:53pm
Just went through the program and changed every * * to * and removed every lone *. Works, I appreciate it cire.
Topic archived. No new replies allowed.