0

我正在开发一个使用 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 语句设置。)有什么想法吗?

4

2 回答 2

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;
}
于 2014-04-12T21:08:45.053 回答
0
 coeff_Matrix = (double**) malloc(nx*sizeof(double*));

您不需要从malloc. 显式类型转换不被认为是一种好习惯。double *如果您使用类型转换,它也应该是。但显式类型转换并不是一个好习惯。所以最好将输出分配malloc给一个指针。类型转换将被隐式处理。

于 2014-04-12T16:41:33.363 回答