2014-04-12 67 views
0

我正在使用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语句设置)任何想法?

+0

你投'coeff_Matrix'用'(双**)',但它是'double *'。但是:[不要强制转换'malloc()']的结果(http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 – pzaenger

+1

如果有人*输入了'0',我想'nx'和'ny'只是“知道”他们应该是1024个提示符?你可能想验证这两个'scanf'调用,你是'malloc' *工作*,或者招致[第六诫命](http://www.seebs.net/c/10com.html) 。没有看到'PDE_Calculate'和'jacobi_Calculate',我不得不假设他们期待一个完全分配的'nx * ny'指针数组,而你不*给出这个。 – WhozCraig

+0

嗯,这里可能更容易向你展示那些参数期望: 无效PDE_calculate(INT NX,INT NY,双**矩阵) 和 无效jacobi_Calculate(双**矩阵,INT NX) –

回答

0
coeff_Matrix = (double**) malloc(nx*sizeof(double*)); 

你不需要键入从malloc转换返回的指针。明确地键入转换并不被认为是一种好的做法。如果您使用类型转换,它应该是double *。但它不是明确键入强制转换的好习惯。所以最好将malloc的输出分配给一个指针。类型转换将被隐式处理。

+0

所以...应该是: 'coeff_Matrix =(double **)malloc(nx * sizeof(double));'? 这就是你要说的吗?看到我对上面的WhozCraig的评论; PDE_calculate和jaboci_Calculate都需要一个参数,如 “double ** Matrix”。 我想我不清楚你在说什么malloc语句应该看起来像... –

1

看来你已经混淆了指针的整个想法。

为了动态声明大小的多维数组(M,N),这样做:

int **array; 
int m=4,n=3,i; 

array=malloc(sizeof(int *)*m); 
for (i=0;i<m;i++) 
    array[i]=malloc(sizeof(int)*n); 

所以,因为这会改变你的程序:

int main(void) { 
double** coeff_Matrix; 
int nx, ny, i; 

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*)); // you don't need to cast the result of malloc though 

    for (i=0;i<nx;i++) 
     coeff_Matrix[i]=(double *)malloc(ny*sizeof(double)); 

    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) 
      { 
       for (i=0;i<nx;i++) 
        free(coeff_Matrix[i]); 

       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; 
} 
+0

现在我收到以下错误: NewtonsMethod.c:71:22:错误:在分配类型时不兼容的类型'double'from type'double *' –

+0

你的代码中的哪一行是? –

+0

'for(row = 0; row