Ok basically I have a program that is doing a counter clockwise spiral and will print the value i or print out a 0 depending upon if the number is prime. n is equal the length of the spiral while i is the initial center value of the spiral which is incremented with each shift in the spiral. I need the spiral to insert the values counter clockwise starting at the center. The problem I'm having is a segmentation fault. When I put it through a debugger it's pointing to the second else statement:

else a[k][l] = 0 ;

but I'm not really sure whats wrong with the pointers. Anyone got any ideas? I tried to snip the code a little bit below. I cut out the parts I knew were working.

n = 5 ;

  i = 16 ;

  in = 2 ;

  m = i + ( (n * n) - 1 ) ;

  k = n / 2 + 1 ;

  l = n / 2 + 1  ;

  a = (int **) malloc( sizeof( int * ) * n ) ;

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

  while ( i < m + 1 ) {

    for (j = 0 ; j < in ; j++, i++, l++) {

      if ( IsPrime(i) ) a[k][l] = i ;

      else a[k][l] = 0 ;

      if ( j == in - 1 ) break ;

    }

    i++ ;

    k++ ;

    for (j = 0; j < (in-1); j++, i++, k++) {

      if ( IsPrime(i) ) a[k][l] = i ;

      else a[k][l] = 0 ;
      if ( j == ( in - 1 ) - 1 ) break;

    }

    i++ ;

    l-- ;

    in++ ;

    for (j = 0; j < in; j++, i++, l--) {

      if ( IsPrime(i) ) a[k][l] = i ;

      else a[k][l] = 0 ;

      if ( j == in - 1 ) break;

    }

    i++ ;

    k-- ;

    for (j = 0; j < (in-1); j++, i++, k--) {

      if ( IsPrime(i) ) a[k][l] = i ;

      else a[k][l] = 0 ;

      if ( j == ( in - 1 ) - 1 ) break;

    }

    i++ ;

    l++ ;

  }

So it looks like you're going out of the bounds of the array.

Try to find out what the values of k and l are when the code fails. You could use the debugger, or just put a printf("%d %d\n,k,l); before the if-statement and trace what the values are while the code is running. If you see where the numbers are running out of bounds, you can probably find the flaw in the code.

Thank you Ishara.

After looking at the debugger again and making a printf to see right before it gives an error it was actually pointing to this full piece of if else statement now. This is the first fragment where it goes to the right. It gives me [3,3] as the value for the array before it fails, which kind of confuses me on how it could go out of bounds since in this case the array is [5][5]

if ( IsPrime(i) ) a[k][l] = i ;

      else a[k][l] = 0 ;

Oh wow... I didn't see this...

Look at line 15, where you build the 'rows' of your matrix. for ( x = 0 ; x < n ; x++ ) a[i] = (int *) malloc( sizeof(int) * n ) ; Shouldn't you be setting a[x] to that value, and not a? :)

Wow. Thanks. It prints now, but not correctly. The spiral keeps screwing up so I tried to rework the spiral part of the code. When it tries to go back to the while part of the loop it gives a segmentation fault. If I break the loop before it hits 25 it will print that part of the spiral correctly.

while ( i < m ) {

    for (j=0;j<in;j++,l++,i++) {
      a[k][l]=i;
    }

    k-- ;
    l--;
    in++;


    for (j=0;j<in;j++,l--,i++) {
      a[k][l]=i;
    }

    k++;

    l++;

    in--;

    for (j=0;j<in;j++,k++,i++) {
      a[k][l]=i;
    }

    k--;

    l++;

    in++;

    in++;

  }

What I'm basically trying to do is this:

32 31 30 29 28
33 20 19 18 27
34 21 16 17 26
35 22 23 24 25
36 37 38 39 40

but I can only get it up to 25 before it hits the while loop again and I don't know why it produces an error. Does anyone have any ideas as to why? The array and printing the array out is fine I know it's just getting the values into the array is the problem.

Try asserting your indices (everywhere) ..

#include <assert.h>

while ( i < m ) {

  for (j=0;j<in;j++,l++,i++) {

    /* rows and cols are your allocated dimensions ... */

    assert(k >= 0 && k < rows);
    assert(l >= 0 && l < cols);

    a[k][l]=i;
  }
This article has been dead for over six months. Start a new discussion instead.