1

I'm a bit confused with memory allocation.

I want to fill a structure Sudoku with random number and then check if a box of 9 numbers are correct.

#define SUDOKU_SIZE 9 
typedef struct 
{ 
   int grid[SUDOKU_SIZE][SUDOKU_SIZE]; 
} sudoku_t;

int main(int argc, char const *argv[]){
    sudoku_t *s=malloc(sizeof s);
    s->grid[0][0]=6;//manualy setting the value of the sudoku
    ...
    s->grid[8][8]=7;
    fill_sudoku_test(s);//fill s, a feasible Sudoku with random number
    int k, l;
    for(k=0;k<SUDOKU_SIZE;k+=3){
        for(l=0;l<SUDOKU_SIZE;l+=3){
            if(correct_box(s,k,l))//check if a box of 3 by 3 contains 9 differents numbers
                printf("Box correct at position :%d and %d\n",k,l);
         }
    }
    free(s);
    return EXIT_SUCCESS;
}

When I compile this code, I got a core dumped error.

If somebody got a solution, I'm interested

EDIT

Here's the others functions :

void fill_sudoku_test(sudoku_t *s){
   int k, l;
   time_t t;
   srand((unsigned) time(&t));
   for(k=0;k<SUDOKU_SIZE;k++){
       for(l=0;l<SUDOKU_SIZE;l++){
           if(!(s->grid[k][l])) {
               s->grid[k][l]=rand()%SUDOKU_SIZE+1;
           }            
       }
   }
}


int correct_tab(int value[]){
   int i;
   int tab[9];
   for(i=0;i<SUDOKU_SIZE;i++){
      tab[i]=0;
   }
   for(i=0;i<SUDOKU_SIZE;i++){
      if(tab[value[i]-1]==1){
        return 0;
      }
      else{
        tab[value[i]-1]=1;
      }
   }
   return 1;
}

int correct_box(sudoku_t *s, int i, int j){
   int tab[SUDOKU_SIZE];
   int count=0;
   int k,l;
   for(k=0;k<3;k++){
      for(l=0;l<3;l++){
         tab[count]=s->grid[i+k][j+l];
      }
   }
   return (correct_tab(tab));
}
LPs
  • 16,045
  • 8
  • 30
  • 61
Cariamole
  • 296
  • 2
  • 10
  • Are you probably missing the grid allocation? Post the `sudoku_t` structure. And `fill_sudoku_test` code. – LPs May 16 '16 at 10:08

3 Answers3

5
sudoku_t *s=malloc(sizeof s);

should be

sudoku_t *s=malloc(sizeof(sudoku_t));

or

sudoku_t *s=malloc(sizeof(*s));

EDIT

in function correct_tab the value[i] can be (and is) 0. Then:

tab[value[i]-1]=1;

and

if(tab[value[i]-1]==1)

access the array out of bounds.

EDIT 2

In correct_tab value array is not intited for the whole SUDOKU_SIZE then some values of array are undefined.

You can, at least, declare it as:

int tab[SUDOKU_SIZE] = {0};

EDIT3

To answer to your comments:

Your init is correct: init numbers are between 1 to 9.

The problem is that correct_tab is called from correct_box is passing tab, a local (stack allocated) array. This means 1 main thing:

  • its values are not itited to 0. Those value are randoms due to the stack allocation.

using my EDIT2 code you can set 0 for all values of the array.

BTW your correct_tab function loops the whole tab array, where only some values are extracted from your sudocu_t struct matrix. This happend due to:

  1. count variable into correct_box function is always 0. You must inc it each time you set a value into tab array.
  2. You should pass the count value to correct_tab function to allow to loop on the real inserted values only.
LPs
  • 16,045
  • 8
  • 30
  • 61
  • As I commented: post other functions. Where is the core dump triggered? – LPs May 16 '16 at 10:12
  • Okay, I did it. And the core dumped come after the fill_sudoku_test – Cariamole May 16 '16 at 10:29
  • Preserving the original style, the allocation could be written as `sudoku_t *s=malloc(sizeof *s);` – martinkunev May 16 '16 at 10:39
  • @LPs , I'm not agree because value[i] can only be numbers between 1 and 9 inclusive – Cariamole May 16 '16 at 10:41
  • Put a `printf("value[i] = %d\n", value[i]);` inside your loop and you will see... – LPs May 16 '16 at 10:42
  • Okay, I'm believing you... But I still don't understand, when I fill s, it only use number from 1 to 9. After that correct_box fill a tab with these numbers, and then, it use correct_tab(tab), why are there numbers like 0 or 4196074.. ? – Cariamole May 16 '16 at 11:00
  • 1
    Yeah, thank you a lot for your explanations... and your time! – Cariamole May 16 '16 at 12:42
1

When you allocate:

sudoku_t *s=malloc(sizeof s);

This only allocates memory sufficient to store a pointer. You probably mean:

sudoku_t *s=malloc(sizeof(*s));

Beyond that, you will need to use a debugger to identify which part of the code actually triggered the segmentation fault.

jxh
  • 69,070
  • 8
  • 110
  • 193
0

First problem lies in this line:

sudoku_t *s=malloc(sizeof s);

which means malloc is allocating memory equal to the size of pointer, which could be 4 bytes or 8 bytes depending on the machine.

Ideally what u r expecting is allocating the size of structure, so this should be:

sudoku_t *s = (sudoku_t *) malloc(sizeof(sudoku_t));
S. Singh
  • 89
  • 1
  • 9
  • [do-i-cast-the-result-of-malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LPs May 16 '16 at 12:07
  • Normally as per the prototype of malloc, it returns a void pointer. So I think it's good to cast it to the structure type. – S. Singh May 16 '16 at 12:30
  • Take a look at the linked SO post. It gives you all infos. – LPs May 16 '16 at 12:33