Dynamic allocation of 2 dim. arrays in classes

This is a discussion on Dynamic allocation of 2 dim. arrays in classes within the C++ Programming forums, part of the General Programming Boards category; I had written a C++ program to manipulate two dimensional matrices using classes. Initially all the arrays within the class ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    Dynamic allocation of 2 dim. arrays in classes

    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.
    Attached Files Attached Files

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you have no copy constructor, so your return temp; will return bitwise copy of the temp matrix.
    with pointers pointing to the deleted memory

    your operator= does not checks that dimention of the matrix could change and as so - you need to delete the old matrix and allocate the new with correct dimention.

    I suppose you have other memory allocation/dealloaction problems...

    Better check all your code for constructors/destructors and operator =
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    I added a copy constructor to both programs and I get the same problem.

    My question is why does dynamic_array_trial.cpp compile and run without any error while runge_kutta1.cpp gives a seg fault? Both programs have identical (copied and pasted) matrix functions.

    Like you said temp being a local object is deleted and I can see that by printing it within the inverse function of runge_kutta1.cpp before it gives seg fault. This should happen with the other program also.

    Thanks.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by circuitbreaker View Post
    I added a copy constructor to both programs and I get the same problem.

    My question is why does dynamic_array_trial.cpp compile and run without any error while runge_kutta1.cpp gives a seg fault? Both programs have identical (copied and pasted) matrix functions.

    Like you said temp being a local object is deleted and I can see that by printing it within the inverse function of runge_kutta1.cpp before it gives seg fault. This should happen with the other program also.

    Thanks.
    Using deleted pointers is undefined behaivior. So it CAN seg fault, as well as CAN do anything else. So your "running fine" application - is just question of luck.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Thanks vart.

    In C I used functions where the return array and the arrays to be operated on were passed as references. That code though working under many different conditions is a little hard to read. The code in C++ resembles the equations we normally write but that means the functions return temporary objects.

    I guess I'll run into this seg fault whenever I use pointers in this manner. I'll keep reading and try out the vectors class.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  3. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. Classes and char* and Dynamic memory allocation
    By Bozz in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2003, 02:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21