0

I am working on a program that uses Jacobi iteration (http://en.wikipedia.org/wiki/Jacobi_iteration). But I'm getting a seg fault. The code looks correct to me, and I'm thoroughly frustrated at this point. Maybe someone can point out my mistake.

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;

} //end main

So, as you can see, the program asks for x-points and y-points. (Tolerance is already set via a #define statement.) Any ideas?

Samuel Brockmann
  • 113
  • 1
  • 1
  • 7
  • You cast `coeff_Matrix` with `(double**)`, but it is `double*`. However: [do not cast the result of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – pzaenger Apr 12 '14 at 16:41
  • 1
    If someone *does* enter `0`, I suppose `nx` and `ny` will just "know" they're supposed to be 1024 as the prompts say? And you may want to validate both those `scanf` calls and you're `malloc` *worked*, or incur the penalties of [The Sixth Commandment](http://www.seebs.net/c/10com.html). Without seeing both `PDE_Calculate` and `jacobi_Calculate`, I have to assume they're expecting a fully allocated pointer array of `nx*ny`, and you're *not* giving that. – WhozCraig Apr 12 '14 at 17:03
  • Well, here it might be easier to show you the parameters those expect: void PDE_calculate(int nx, int ny, double** Matrix) and void jacobi_Calculate(double** Matrix, int nx) – Samuel Brockmann Apr 12 '14 at 19:10

2 Answers2

1

It seems you have confused the whole idea of pointers.

To declare a multidimensional array of size (m,n) dynamically, do this:

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);

So this would modify your program as:

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;
}
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
0
 coeff_Matrix = (double**) malloc(nx*sizeof(double*));

you dont need to type cast the returned pointer from malloc. it is not considered as a good practice to type cast explicitly. and also it should be double * if you use type cast. but its not good practice to type cast explicitly. so better just assign the output of malloc to a pointer. type cast will be handled implicitly.

LearningC
  • 3,182
  • 1
  • 12
  • 19
  • So...should it be: `coeff_Matrix = (double**) malloc(nx*sizeof(double));` ? Is that what you're saying? See my comment to WhozCraig above; both PDE_calculate and jaboci_Calculate both expect a parameter like "double** Matrix". I guess I'm unclear on what you're saying the malloc statement should look like... – Samuel Brockmann Apr 12 '14 at 19:13