How to use malloc for a two-dimensional array of structures? (bus error: 10)

I am trying to implement a sudoku solution. To do this, I use the structure shown below to represent a cell on a sudoku board. Then I declare a 9x9 array of these structures to represent the board.

Cell structure:

struct cell{
     char value;
     unsigned int possible;
};

then I declare the sctructs array as:

struct cell board[9][9];

My problem is that when I try to enter a value into an array (for example, board [2] [2] .value = getchar ();), sometimes it works, and sometimes I get this error:

Bus error: 10

I'm not quite sure what this means ... How is "Bus Error: 10" different from a segmentation error?

gcc vim. , . , malloc , :

int ** Array;  
Array = (int**) malloc(x_size*sizeof(int*));  
for (int i = 0; i < x_size; i++)  
    Array[i] = (int*) malloc(y_size*sizeof(int)); 

2- .

- ?

struct cell** board;
board = (struct cell**) malloc(x_size*sizeof(struct cell**));
for(int i=0; i< x_size; i++)
    board[i] = (struct cell*) malloc(y_size*sizeof(struct cell));

, "sizeof (struct cell)" , .

! C (++ - ), C, .
!

!

, , , , , , :

 /* only code being used in solver.h*/
 29 /* structure to describe a cell */
 30 struct cell{
 31     int value;
 32     unsigned int possible;
 33 };



   /*solver.c*/
 4 #include <ctype.h>
 5 #include <stdio.h>
 6 #include "solver.h"
 7 
 8 
 9 struct cell board [9][9];
 10 
 11 
 12 int main(){
 13     initialize_board();
 14     print_board();
 15     setup_board();
 16     print_board();
 17 return 0;
 18 }
 19 
 20 void print_board(){
 21     int i=0, j=0;
 22     for(i = 0; i<9; i++){
 23         for(j = 0; j<9; j++)
 24             printf(" %d",board[i][j].value);
 25         printf("\n");
 26     }
 27 }
 28 
 29 void initialize_board(){
 30     int i = 0, j = 0;
 31 
 32     for(i = 0; i<9; i++)
 33         for(j = 0; j<9; j++){
 34             (board[i][j]).value = 0;
 35             (board[i][j]).possible = 0x1FF;
 36         }
 37 }
 38 
 39 void setup_board(){
 40     int row=0, col=0, val = 0;
 41     char another = 'Y';
 42 
 43     printf("Board Initial Setup.\nEnter the row and column number of the value to be entered into the board.");
 44     printf("\nRow and Column indexes start at one, from top left corner.");
 45     while(another == 'Y'){
 46         printf("\nRow: ");
 47         row = getchar();
 48         printf("Column: ");
 49         getchar();
 50         col = getchar();
 51         printf("Value: ");
 52         getchar();
 53         (board[row-1][[col-1]).value = getchar();
 54         printf("Enter another value? (y/n): ");
 55         getchar();
 56         another = toupper(getchar());
 57         getchar();
 58     }
 59 }

, int, getchar(). - / . , while setup_board say Row: 1, Col: 1, Value: 5, , "n", , 5 , . initialize_board().

:

Board Initial Setup.
Enter the row and column number of the value to be entered into the board.
Row and Column indexes start at one, from top left corner.
Row: 1
Column: 1
Value: 4
Enter another value? (y/n): n
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0

, , : :

Board Initial Setup.
Enter the row and column number of the value to be entered into the board.
Row and Column indexes start at one, from top left corner.
Row: 5
Column: 5
Value: 5
Bus error: 10

, getchar(), .
!

getchar()... , ASCII- . , :

 47     while(another == 'Y'){
 48         valid=0;
 49         while(!valid){
 50             printf("\nRow: ");
 51             row = getchar() - '0';  /* convert ASCII character code to actual integer value */
 52             getchar();              /*added to remove extra newline character from stdin */
 53             printf("Column: ");
 54             col = getchar() - '0';
 55             getchar();              /*remove \n */
 56             printf("Value: ");
 57             val = getchar() - '0';
 58             getchar();              /*remove \n */
 59             if(val >9 || val<1 || col>9 ||col<1 || row>9 || row<1)
 61                 printf("\nInvalid input, all values must be between 1 and 9, inclusive");
 62             else
 63                 valid = 1;  
 64         }
 65         board[row-1][col-1].value = val;
 66         printf("Enter another value? (y/n): ");
 67         another = toupper(getchar());
 68         getchar();                  /*remove \n */
 69     }

, , , !

!

, - , ?

, , , , , ?

+3
5

getchar , ; , . , , :

printf("\nRow: ");
row = getchar();
printf("Column: ");
getchar();

, "hehe" 1 1, ? ASCII h e, row col , , .

, ! 1, ASCII (49) :

board[49-1][49-1].value = 53;

, :

if (row >= '1' && row <= '9')
    row -= '0'; // a common C hack for converting a character-code to a number
else
    printf("Bad input"); // not a proper error handling, just an example

col -= '0'; // 1 line for brevity; you must do the same code as above

value = getchar();
value -= '0'; // same as above

board[row-1][col-1].value = value;
+2

getchar() int. , char. getchar()

+2

-, . malloc C:

Type *ptr;
ptr = malloc (n * sizeof(*ptr));

sizeof , . ptr, , - Type, Type*. , void* . , :

#define ALLOC(p, n) p = malloc(n * sizeof(*p))

, :

Type **matrix;
matrix = malloc(row * sizeof(*matrix));
matrix[0] = malloc(row * col * sizeof(*matrix[0]))
for (i=0; i < row; i++){
    matrix[i] = matrix[0] + i*col;
}

, , , - . , matrix[i][j]. matrix[i*col + j], .

, , , ,

typedef struct Cell Cell;
struct Cell{
    ...
};

Cell board[9][9];

, . char, . - - getchar? .

+2

Q, , , malloc ..

malloc .

, , int, N. char int.

, - , , , .


malloc . , , . , . . . .

int * int. , LSB 4 4 int.


, , malloc. , , grep .., /.

, , . .. 6 - , , , . malloc, , , MYMALLOC, .

, . .

:

BLAH(k, v);
while (--i)
     BOFF(mak, VED(uu));
if (FOO != k)
      BAR;

, , , , . . , .

.


typedefs, . struct.

, struct foo bar; vs BLAH bar;. . , unsigned char, , .. . , struct, , , , , , .

, , . , .

Typedefs , , , .


#define. .

+1

I do not know why you get the error Bus 10:, it should work fine with a static array.

But for dynamic memory allocation for boardyou can use:

cell ** board = (cell **) malloc(x_size * sizeof(cell *));

for (int i = 0; i < Size_X; i++)
{
    board[i] = (cell *) malloc (y_size * sizeof (cell));
}

You can also allocate boardas a simple array of 81 cell:

cell * board = (cell *) malloc (x_size * y_size (cell));

I tested static distribution under Windows and it works fine since I tested above with malloc, but my compiler is configured in C ++, so the above code may not work 100% with a pure C compiler.

Hope this helps.

0
source

All Articles