Hello
I write program that can solve linear equations with LUP decomposition method.I know that my algorithm is true and every part of my program do what I want. when I call function lupDecomposition() inside the lupSolve() the program crash.This is my related code:

int makeUpperDiagonal(Matrix * src){
		int i,j;
		for(i = 0;i < src->rowCount;i++){
			for(j = 0; j <src->colCount;j++){
				if(i > j)
					src->elements[i][j] = 0;
			}
		}
		return 0;
	}

	int makeLowerDiagonal(Matrix * src){
		int i,j;
		for(i = 0;i < src->rowCount;i++){
			for(j = 0; j< src->colCount; j++){
				if(i < j)
					src->elements[i][j] = 0;
			}
		}
		return 0;
	}
	int lupDecompose(Matrix* mat,MyVector* p,Matrix* u,Matrix* l){
		int i,k,j,index;
		double pivot;
		for(i= 0; i< mat->colCount-1;i++){
			p->elements[i] = i;
		}
		for( k = 0; k < mat->colCount;k++){
			pivot = 0;
			for( i = k; i < mat->colCount;i++){
				if(mat->elements[i][k] > pivot){
					pivot = mat->elements[i][k];
					index = i;
				}
			}
//			if(pivot == 0){
//				printf("Singular Matrix Entered");
//			}
			vectorItemExchange(p,k,index);
			for(i = 0; i < mat->colCount;i++){
				itemEchange(mat,k,i,index,i);
			}
			for(i = k+1; i < mat->colCount;i++){
				mat->elements[i][k] = mat->elements[i][k]/mat->elements[k][k];
				for(j = k+1;j < mat->colCount;j++){
					mat->elements[i][j]= mat->elements[i][j] - mat->elements[i][k] * mat->elements[k][j];
				}

			}

		}
		//matrixPrint(mat);
		printf("\n");
		for(i = 0; i < mat->rowCount;i++){		//copying main matrix item to u and l
			for(j = 0; j< mat->colCount;j++){
				u->elements[i][j] = mat->elements[i][j];
				l->elements[i][j] = mat->elements[i][j];
			}
		}
		printf("\n");
		makeUpperDiagonal(u);
		makeLowerDiagonal(l);
		matrixPrint(u);
		printf("\n");
		matrixPrint(l);
		printf("\n");
		return 0;
	}

int lupSolve(Matrix* mat,MyVector* b,MyVector* x){
		int i,j;
		double t;
		MyVector* y;
		y = malloc(sizeof(MyVector));
		newVector(mat->rowCount,y);
		MyVector* p;
		p = malloc(sizeof(MyVector));
		newVector(3,p);
		Matrix* u;
		u = malloc(sizeof(Matrix));
		newMatrix(3,3,u);
		Matrix* l;
		l = malloc(sizeof(Matrix));
		newMatrix(3,3,l);
		[B]lupDecompose(mat,p,u,l);
		printf("I am here");[/B]
		for(i= 0; i < mat->rowCount;i++){
			for(j = 0; j < i-1;j++){
				t = l->elements[i][j] * y->elements[j];
			}
			int num;
			num = p->elements[i];
			y->elements[i] = b->elements[num] - t;
		}
		for(i = mat->rowCount; i > 1  ;i--){
			for(j = i+1; j < mat->rowCount ;j++){
				t = u->elements[i][j] * x->elements[j];
			}
			x->elements[i] = (y->elements[i] - t) / (u->elements[i][i]);
		}
		return 0;
	}

I aspected that the statement "I am here" in function lupSolve() be written on console but it seems that the program running is stopped without any error

Recommended Answers

All 5 Replies

First thing - are you getting any warnings from your compiler? And do you have your warning level turned up high?

Second, try checking the return addresses that malloc is giving your pointers. Are any of them null (indicating a serious problem)?

Third, cut down on the space where the error could be. Move your "I am here", up by 5 lines of code, until it becomes visible - now you know the error is between that line of code and the 5 lines of code further down.

Good hunting.

I didn't gett any warning and I didn't change compiler default settings.I use GCC c compiler.I try to check the pointers addresses so I add two line of code to my last code as below:

int lupDecompose(Matrix* mat,MyVector* p,Matrix* u,Matrix* l){
		int i,k,j,index;
		double pivot;
		for(i= 0; i< mat->colCount-1;i++){
			p->elements[i] = i;
		}
		for( k = 0; k < mat->colCount;k++){
			pivot = 0;
			for( i = k; i < mat->colCount;i++){
				if(mat->elements[i][k] > pivot){
					pivot = mat->elements[i][k];
					index = i;
				}
			}
//			if(pivot == 0){
//				printf("Singular Matrix Entered");
//			}
			vectorItemExchange(p,k,index);
			for(i = 0; i < mat->colCount;i++){
				itemEchange(mat,k,i,index,i);
			}
			for(i = k+1; i < mat->colCount;i++){
				mat->elements[i][k] = mat->elements[i][k]/mat->elements[k][k];
				for(j = k+1;j < mat->colCount;j++){
					mat->elements[i][j]= mat->elements[i][j] - mat->elements[i][k] * mat->elements[k][j];
				}

			}

		}
		//matrixPrint(mat);
		printf("\n");
		for(i = 0; i < mat->rowCount;i++){		//copying main matrix item to u and l
			for(j = 0; j< mat->colCount;j++){
				u->elements[i][j] = mat->elements[i][j];
				l->elements[i][j] = mat->elements[i][j];
			}
		}
		printf("\n");
		makeUpperDiagonal(u);
		makeLowerDiagonal(l);
		matrixPrint(u);
		printf("\n");
		matrixPrint(l);
[B]		printf("Address of u pointer in lup decomposition:%p\n",&u);
		printf("Address of l pointer in lup decomposition:%p\n",&l);[/B]
		printf("\n");
		return 0;
	}

	int lupSolve(Matrix* mat,MyVector* b,MyVector* x){
		int i,j;
		double t;
		MyVector* y;
		y = malloc(sizeof(MyVector));
		newVector(mat->rowCount,y);
		MyVector* p;
		p = malloc(sizeof(MyVector));
		newVector(3,p);
		Matrix* u;
		u = malloc(sizeof(Matrix));
		newMatrix(3,3,u);
		Matrix* l;
		l = malloc(sizeof(Matrix));
		newMatrix(3,3,l);
[B]		printf("Address of u pointer:%p\n",&u);
		printf("Address of l pointer:%p\n",&l);[/B]
		lupDecompose(mat,p,u,l);
		printf("I am here");
		for(i= 0; i < mat->rowCount;i++){
			for(j = 0; j < i-1;j++){
				t = l->elements[i][j] * y->elements[j];
			}
			int num;
			num = p->elements[i];
			y->elements[i] = b->elements[num] - t;
		}
		for(i = mat->rowCount; i > 1  ;i--){
			for(j = i+1; j < mat->rowCount ;j++){
				t = u->elements[i][j] * x->elements[j];
			}
			x->elements[i] = (y->elements[i] - t) / (u->elements[i][i]);
		}
		return 0;
	}

what I get was
Address of u pointerin lupSolve():0x7fff5fbffa20
Address of l pointer lupSolve():0x7fff5fbffa18
...
...
Address of u pointer in lupDecomposition():0x7fff5fbff9a8
Address of l pointer in lupDecomposition():0x7fff5fbff9a0

As you see the address of u and l pointer are different in two function I don't know is it the problem or not ?

I think that the pointer difference addresses is not the problem it should have logical problems I think because every part is doing its work true separately.

Correct. As long as the malloc() calls are not returning NULL, you should be getting valid addresses - so that's not the problem.

Aren't you getting a warning about the variable p, l, and u, in lupSolve()? I don't see a declaration for them in that function. If they are globals (bad!), then it's very poor form to pass them around to the functions as parameters - those are copies of the global, and as such, their value will be lost when the function they're passed to, is done. (That is, they will return to their global instantiation, with their global values).

As I tell you I didn't get any warning. I declare the variables l ,u and p local by this codes:

MyVector* p;
		p = malloc(sizeof(MyVector));
		newVector(3,p);
		Matrix* u;
		u = malloc(sizeof(Matrix));
		newMatrix(3,3,u);
		Matrix* l;
		l = malloc(sizeof(Matrix));
		newMatrix(3,3,l);

I think the problem maybe about garbage collecting but I am not sure about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.