01/10/2018, 12:30

Sửa lỗi segmentation fault (core dumped)

bài của mình nó bị lỗi này lúc bắt đầu chạy
các bạn giúp mình sửa lỗi này được không?
tối 11h phải nộp bài oy

phần code của mình đây:

include "stdio.h"
include "pthread.h"
include "stdlib.h"

struct arr{
	int row,col;
	int **a;
};

struct file{
	struct arr a,b,c;
	char* filename;
	char* output;
};

void* nhapMaTran(void* ar){
	struct file *fi = (struct file*) ar;
	FILE *in;
	in = fopen(fi->filename,"r");
	//so hang cua ma tran A
	fscanf(in,"%d",&fi->a.row);
	//so cot cua ma tran A 
	fscanf(in,"%d",&fi->a.col);
	fi->a.a = (int**)malloc(fi->a.row*sizeof(int *));
	for(int i=0;i<fi->a.col;i++)
		fi->a.a[i] = (int*)malloc(fi->a.col*sizeof(int));
	for(int i=0;i<fi->a.row;i++)
		for(int j=0;j<fi->a.col;j++)
			fscanf(in,"%d",&fi->a.a[i][j]);
	
	//so hang cua ma tran B
	fscanf(in,"%d",&fi->b.row);
	//so cot cua ma tran B
	fscanf(in,"%d",&fi->b.col);
	fi->b.a = (int**)malloc(fi->b.row*sizeof(int *));
	for(int i=0;i<fi->b.col;i++)
		fi->b.a[i] = (int*)malloc(fi->b.col*sizeof(int));
	for(int i=0;i<fi->b.row;i++)
		for(int j=0;j<fi->b.col;j++)
			fscanf(in,"%d",&fi->b.a[i][j]);
	fclose(in);
}

void* nhanMaTran(void* ar){
	struct file *fi = (struct file*) ar;
	fi->c.row = fi->b.row;
	fi->c.col = fi->a.col;
	fi->c.a = (int**)malloc(fi->c.row*sizeof(int*));
	for(int i=0;i<fi->c.col;i++)
		fi->c.a[i] = (int*)malloc(fi->c.col*sizeof(int));
	for(int i=0;i<fi->c.row;i++)
		for(int k=0;k<fi->c.col;k++){
			fi->c.a[i][k] = 0;
			for(int j=0;j<fi->a.row;j++)
				fi->c.a[i][k] += fi->a.a[i][j]*fi->b.a[j][k];
		}

}

void* ketQua(void* ar){
	struct file *fi = (struct file*) ar;
	FILE *out;
	out = fopen(fi->output,"wb");
	for(int i=0;i<fi->c.row;i++){
		for(int j=0;j<fi->c.col;j++){
			fprintf(out,"%d",fi->c.a[i][j]);
			fprintf(out," ");
		}
		fprintf(out,"
");
	}
	
	fprintf(out,"
");
	fclose(out);
}

int main(int argc,char* argv[]){
	pthread_t tid[3];
	struct file fi;
	int status,*pstatus = &status;
	fi.filename = argv[1];
	pthread_create(&tid[0],NULL,nhapMaTran,(void*) &fi);
	sleep(1);

	if(pthread_join(tid[0],(void**) pstatus)==0){
		int tmp = pthread_create(&tid[1],NULL,nhanMaTran,(void*) &fi);
		if(tmp==0 && pthread_join(tid[1],(void**) pstatus)==0){
			fi.output = argv[2];
			pthread_create(&tid[2],NULL,ketQua,(void*) &fi);
		}
	}
	sleep(2);
}
HK boy viết 14:32 ngày 01/10/2018
  • Format lại code bằng cách thêm 3 dấu ` vào đầu và cuối code, như thế này:

// code

  • Đề bài đâu hả bạn? Bạn up mỗi code không thì chẳng ai giúp được bạn đâu.
*grab popcorn* viết 14:46 ngày 01/10/2018

A post was merged into an existing topic: Topic chứa các reply được cho là off-topic - version 2

Ha Vinh Tuyen viết 14:44 ngày 01/10/2018

đề của mình đây bạn: Ứng dụng multithread trong bài toán nhân ma trận. Đọc vào 2 ma trận A & B từ file, sau đó dùng nhiều thread để thực hiện việc nhân 2 ma trận này. Ghi ma trận kết quả ra một file khác

HK boy viết 14:40 ngày 01/10/2018
for(int i=0;i<fi->a.col;i++)
	fi->a.a[i] = (int*)malloc(fi->a.col*sizeof(int));
for(int i=0;i<fi->b.col;i++)
	fi->b.a[i] = (int*)malloc(fi->b.col*sizeof(int));

Bạn đọc kĩ đoạn này xem.

Ha Vinh Tuyen viết 14:42 ngày 01/10/2018

mình hiu oy
để mình chạy lại thử
cảm ơn bn trc

Ha Vinh Tuyen viết 14:35 ngày 01/10/2018

mình sửa lại thành
for(int i=0;ia.row;i++)
fi->a.a[i] = (int*)malloc(fi->a.colsizeof(int));
for(int i=0;ib.row;i++)
fi->b.a[i] = (int
)malloc(fi->b.col*sizeof(int));
mà nó vẫn bị lỗi bạn.

HK boy viết 14:32 ngày 01/10/2018
for(int j=0;j<fi->a.row;j++)
	fi->c.a[i][k] += fi->a.a[i][j]*fi->b.a[j][k];

Sai công thức nhân ma trận.

Ha Vinh Tuyen viết 14:43 ngày 01/10/2018

thấy trên mạng cũng dùng công thức nên mình dùng theo

Ha Vinh Tuyen viết 14:37 ngày 01/10/2018

mình sửa dc oy bạn.
cảm ơn bạn nhiu.

Bài liên quan
0