segmentation fault in Matrix implimentation.

Hello guys.
When i try to run my code i get the Segmentation fault error. i cant see to find where the problem is....
my goal is to read from a txt file as bellow
2
1 0
0 1
3
1 2 3
3 6 6
5 5 4
as You can see the input file first gives the size of the array(always n x n) then gives the matrix its self. Am sure first i need to make an array that is n x n then import in those empty memory slots the values of the matrix. here is my code. towards the end is where my problem starts....check my second post for code




thanks for any ideas
Last edited on
There are a couple of problems at first glance.

The seg fault is caused by lines 49-50. Line 50 declares a 2d array with dimensions sizexsize, except that size is unintialized so who knows how big or small it is.

Another problem is that your indention of lines 39 and 41 does not reflect the flow of execution. I suspect you intended to do all three prints in the else case, but you need braces.

Another problem is that even if you fail to open either the input or the output file, you still continue on.

Another problem is that your for() loops go from 0...size inclusive, but the valid array indices are 0...size-1 inclusive.
i see what you mean. i need to do dynamic 2d array that creates the matrices on the fly.
given
2
1 0
0 1
3
1 2 3
3 6 6
5 5 4
when i infile>>size it should automaticly create a 2x2 matrices and read the data after. here is the part am stuck on..i still need to do alot of calculations with this input so am kinda stuck to move on.please help if u can thanks jsmith
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	
        //I/o

	int **data;
	int size;
	
	inFile >> size;
	cout<<size<<endl;   //checkpoint
	
	data = new int*[size];
	cout <<data<<endl;
	for (int i=0; i<size; i++)
	{
		data[i] = new int[size];
	}
	
	for (int k=0; k<size; k++)
	for (int j=0; j<size; j++)
	{
	inFile >> data[k][j];
	}	
	cout <<data[k][j]<<endl;
return 0;
}


The code you pasted above looks correct at first glance.

What do you need help with specifically?
i need help making matrix constructor of the given size... that takes an int
Post your matrix class declaration and implementation and we'll go from there.
well my constructors look like this i havent developed em yet since i cant even import my matrix with given size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MatrixCalculator{
public:
double m[ ][ ]
		
MatrixCalculator(int n);	  // am not sure which one to use the top or this one.....plus how the to allocate memory for array   size n given for this constructor.
		
int determinant()
	
Matrixcalculator inearMatrixs(int r, int c);       //still have issues going through the array like nesting 2x2 out of  8x8	
 
friend ostream& operator<<(ostream& os, Matrix& matrix);

friend istream& operator>>(istream& is, Matrix& matrix);
}


Your code above shows how to create the matrix dynamically using

1
2
int** data;
int     size;


so make data and size class data members instead of m and then copy your above code into your constructor.

Then write a destructor that frees all the memory.

http://www.cplusplus.com/forum/articles/7459/

Also, Boost has a Matrix object in it's UBLAS library if you want an already built implementation.
Topic archived. No new replies allowed.