分段错误(Malloc?)

时间:2014-04-12 16:38:32

标签: c malloc newtons-method

我正在开发一个使用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语句设置了容差。)任何想法?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

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