我正在使用Jacobi迭代的程序(http://en.wikipedia.org/wiki/Jacobi_iteration)。但我得到一个seg故障。该代码看起来对我来说是正确的,在这一点上我彻底沮丧。也许有人可以指出我的错误。分割错误(Malloc?)
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语句设置)任何想法?
你投'coeff_Matrix'用'(双**)',但它是'double *'。但是:[不要强制转换'malloc()']的结果(http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 – pzaenger
如果有人*输入了'0',我想'nx'和'ny'只是“知道”他们应该是1024个提示符?你可能想验证这两个'scanf'调用,你是'malloc' *工作*,或者招致[第六诫命](http://www.seebs.net/c/10com.html) 。没有看到'PDE_Calculate'和'jacobi_Calculate',我不得不假设他们期待一个完全分配的'nx * ny'指针数组,而你不*给出这个。 – WhozCraig
嗯,这里可能更容易向你展示那些参数期望: 无效PDE_calculate(INT NX,INT NY,双**矩阵) 和 无效jacobi_Calculate(双**矩阵,INT NX) –