[英]Initialize struct with malloc in C
So I have what I think is a noob question. 所以我有一个新手问题。 Sorry for that in advance, and for the grammar as english is not my primary language.
事先对此感到抱歉,因为语法不是英语不是我的主要语言。
So I have to make a game of checkers. 所以我必须做一个跳棋游戏。 I have some struct, defined by
我有一些结构,定义为
struct game {
int **board;
int xsize, ysize;
struct move *moves;
int cur_player;
};
struct coord {
int x, y;
};
struct move_seq {
struct move_seq *next;
struct coord c_old;
struct coord c_new;
int piece_value;
struct coord piece_taken;
int old_orig;
};
struct move {
struct move *next;
struct move_seq *seq;
};
And I have to initialize a struct game wih the fonction 我必须用功能初始化一个结构游戏
struct game *new_game(int xsize, int ysize)
So, here's my problem. 所以,这是我的问题。 I call, for now, new_game always with 10 and 10 values for xsize an ysize.
现在,我称new_game始终将xsize的10和10值设为ysize。 Then I initialize the board game, which I want to assign later.
然后,我初始化棋盘游戏,稍后再分配。
int black = 1;
int white = 5;
int **board;
int i;
int j;
int k;
for(i=0;i<xsize;i++)
{
for(j=0;j<ysize;j++)
{
if(i<(xsize/2) && j<(ysize/2) && (i+j)%2!=0)
{
board[i][j] = black;
}
else if(i>(xsize/2) && j>(ysize/2) && (i+j)%2!=0)
{
board[i][j] = white;
}
else board[i][j] = 0;
}
}
struct game *new = malloc (sizeof(struct game *));
if (new == NULL) return NULL;
So, my problem is after that. 所以,我的问题是在那之后。 I just have Segmentation Fault whatever I do with my struct new.
无论我对struct new做什么,我都遇到了Segmentation Fault。
I tried to do assign new->xsize = xsize and the same with ysize. 我试图分配new-> xsize = xsize,并且与ysize相同。 I do a malloc for the board and the struct move, like I learned to do, but I kept getting this error of Segmentation Fault.
像我学过的那样,我为板和结构移动执行了一个malloc动作,但是我不断遇到Segmentation Fault的错误。
So here's my real question: How to assign and initialize correctly a struct ? 所以这是我真正的问题:如何正确分配和初始化结构? Do I Have to make a malloc for each of the member of struct game ?
我是否必须为struct game的每个成员制作一个malloc? (I tried that too but without any success...)
(我也尝试过,但没有成功...)
I don't necessarily want just the answer, I'd prefer to really understand what I have to do in this case and in general, to make less mistakes in the future. 我不一定只想得到答案,我更希望真正了解在这种情况下的工作,总的来说,以减少将来的错误。
Thanks in advance for your help. 在此先感谢您的帮助。
Have a good day. 祝你有美好的一天。
It happens because you only allocated space for a pointer to your struct
. 发生这种情况是因为您只为指向
struct
的指针分配了空间。 What you need to do, is allocate it for the entire size of it: 您需要做的是为其分配整个大小:
struct game *new = malloc (sizeof(struct game));
Edit: Don't be mislead by the return value of malloc, as it returns a pointer to the allocated space, that's why it should be struct game *new
as it is. 编辑:不要被malloc的返回值误导,因为它返回一个指向分配空间的指针,这就是为什么它应该是
struct game *new
的原因。
In addition to Michael's bug fix, I also wonder about board: it is not allocated, yet you write to it. 除了Michael的错误修复之外,我还想知道板子:它没有分配,但您可以写它。 I think it should be like:
我认为应该是这样的:
struct game *new_game(int xsize, int ysize)
{
int black = 1;
int white = 5;
int i;
int j;
struct game *new = malloc (sizeof(struct game));
if (new == NULL) return NULL;
game->xsize= xsize;
game->ysize= ysize;
game->board= malloc(xsize*ysize*sizeof(int));
for(i=0;i<xsize;i++)
{
for(j=0;j<ysize;j++)
{
if(i<(xsize/2) && j<(ysize/2) && (i+j)%2!=0)
{
game->board[i*xsize+j] = black;
}
else if(i>(xsize/2) && j>(ysize/2) && (i+j)%2!=0)
{
game->board[i*xsize+j] = white;
}
else game->board[i*xsize+j] = 0;
}
}
return (game);
}
Note also the array indexing: the compiler doesn't know the dynamic rowsize so you have to do that yourself. 还要注意数组索引:编译器不知道动态行大小,因此您必须自己做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.