我正在开发一个使用 Jacobi 迭代(http://en.wikipedia.org/wiki/Jacobi_iteration)的程序。但我遇到了段错误。代码对我来说看起来是正确的,此时我感到非常沮丧。也许有人可以指出我的错误。
int main(void) {
double* coeff_Matrix;
int nx, ny;
do{
//Get values of nx and ny from user.
printf("Please enter the number of x-points and the number of y-points desired. \n");
printf("\n");
printf("How many x-points do you want? Enter zero for default (1024). \n");
scanf("%d", &nx);
printf("How many y-points do you want? Enter zero for default (1024). \n");
scanf("%d", &ny);
coeff_Matrix = NULL;
coeff_Matrix = (double**) malloc(nx*sizeof(double*)); //SEGMENTATION FAULT DUE TO THIS?
if(nx > 0) {
PDE_calculate(nx, ny, &coeff_Matrix); //This method is used to generate a diagonally dominant matrix.
jacobi_Calculate(&coeff_Matrix, nx); //This method does the Jacobi iteration.
}
else {
puts("Invalid choice or memory available was exceeded ... Try again.");
if(coeff_Matrix != NULL)
free(coeff_Matrix);
}
}while(more()); //The more() method just asks the user if they would like to do a different problem. User just says "y/n". Mainly here to allow for expanded functionality.
return 0;
} //结束主程序
因此,如您所见,程序要求 x 点和 y 点。(公差已经通过#define 语句设置。)有什么想法吗?