单元格结构体:
struct cell{
char value;
unsigned int possible;
};
然后,我声明了一个结构体数组如下:
struct cell board[9][9];
我的问题是,当我尝试向数组中输入一个值(例如,board[2][2].value = getchar()),有时它可以正常工作,而有时候会出现以下错误:
Bus error: 10
我不太确定这是什么意思... "总线错误: 10"与分段错误有何不同? 我正在使用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));
但是我在实现一个二维结构体数组的内存分配部分时遇到了问题。
是否应该像这样实现?
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++ 是我的母语),我已经使用了嵌入式 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 }
如您所见,我已将value的数据类型更改为int以匹配getchar()的返回类型。但是我的代码仍然产生奇怪的运行时错误/结果。例如,在setup_board的while循环的第一次迭代中,我可以输入:行:1,列:1,值: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
任何关于如何清理这个难看的double 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 }
感谢大家的帮助和意见,我将实施你们提出的许多建议,并且仅从这个问题中就学到了很多!
第三次修改
最后一个问题!
尽管我的原始问题已解决,但是否有人对动态分配内存来实现矩阵有强烈的意见或理由呢?
我想,既然它现在正常工作,我就把它留下来。但是,由于矩阵相当大,动态分配内存会更好吗?