I had written a C++ program to manipulate two dimensional matrices using classes. Initially all the arrays within the class matrix were of fixed dimensions. I am trying out dynamic memory allocation using new. I will use the vector class later but would like to get this dynamic array allocation working.

I have attached two files - dynamic_array_trial.cpp and runge_kutta1.cpp. Both of them contain the same functions. Only the main programs differ. However, I will post the part of the code that creates problems:

Code:
Matrix inverse(const Matrix &mat1) {
	int x,y,z,flag;
	float temp,swap_temp;
	Matrix temp1(mat1.rows, mat1.columns), temp2(mat1.rows, mat1.columns);

// Checking if the matrix is square.
	if (mat1.rows!=mat1.columns) {
		cout << "Matrix is not square.\n";
		exit(1);
	}

	
// Creating a temporary matrix equal to original matrix.
	temp2=mat1;

// Creating an identity matrix.
	temp1=identity(temp2.rows);

	for (x=0;x<temp2.rows;x++) {

// Checking if diagonal element of row x is zero.
		flag=1;
		if (!temp2.mat_var[x][x]) {
			y=x+1;
// Checking if row y > row x has the (y,x) element as non-zero.
			while ((flag)&&(y<=temp2.rows)) {

// If row y exceeds dimensions, then matrix in not invertible.
				if (y>=temp2.rows) {
					cout << "Matrix is not invertible.\n";
					exit(1);
				}

// Otherwise exchange row y and row x of temp1 and temp2.
				if (temp2.mat_var[y][x]) {
					for (z=0;z<temp2.columns;z++) {
						swap_temp=temp2.mat_var[x][z];
						temp2.mat_var[x][z]=temp2.mat_var[y][z];
						temp2.mat_var[y][z]=swap_temp;
						swap_temp=temp1.mat_var[x][z];
						temp1.mat_var[x][z]=temp1.mat_var[y][z];
						temp1.mat_var[y][z]=swap_temp;
					}
					flag=0;	
				}
				y++;
			}
		}


// Making diagonal element of row x unity.
		temp=temp2.mat_var[x][x];
		for (y=0;y<temp2.columns;y++) {
			temp2.mat_var[x][y]=temp2.mat_var[x][y]/temp;
			temp1.mat_var[x][y]=temp1.mat_var[x][y]/temp;
		}

// Making all other elements of column x zero by row operations.
		for (y=0;y<temp2.columns;y++) {
			if (y!=x) {
				temp=temp2.mat_var[y][x];
				for (z=0;z<temp2.columns;z++) {
					temp2.mat_var[y][z]=temp2.mat_var[y][z]-temp*temp2.mat_var[x][z];
					temp1.mat_var[y][z]=temp1.mat_var[y][z]-temp*temp1.mat_var[x][z];
				}
			}
		}
	}

	return temp1;
}
Here, the dynamic allocation is performed within the class Matrix as:

Code:
	void create_matrix() {
		try {
			mat_var=new float* [rows];
		}
		catch (bad_alloc xa) {
			cout << "Out of memory.\n";
			exit(1);
		}

		int row_count;
		for (row_count=0;row_count<rows;row_count++) {
			try {
				mat_var[row_count]=new float [columns];
			}
			catch (bad_alloc xa) {
				cout << "Out of memory.\n";
				exit(1);
			}
		}

		return;
	}

The problem I am facing is that the program dynamic_array_trial.cpp runs perfectly without any error but runge_kutta1.cpp gives a Seg fault at the first calculation of inverse.

If the problem is with declaring a temporary object by dynamic memory allocation then both programs should seg fault. I am using gcc version 4.1.2 on Slackware 12.

Thanks.