[英]ANSI C implementation of Conway's Game of Life
I am currently trying to learn the C language by implementing a console version of "Conway's Game of Life".我目前正在尝试通过实现“Conway's Game of Life”的控制台版本来学习 C 语言。 I have a function called 'set_dead_state', which just create creating a 2d array of 0's:我有一个名为“set_dead_state”的函数,它只是创建一个 0 的二维数组:
void create_game_board(int *dest[], int width, int height) {
for(int i = 0; i < height; i++) {
dest[i] = malloc(sizeof(int)*width);
memset(dest[i], 0, width*sizeof(int));
}
}
Setting width
and height
to 10
, I loop through the array and print out each 0
as " - "
using this function:将width
和height
设置为10
,我遍历数组并使用此函数将每个0
打印为" - "
:
void print_game_board(int* board[], int width, int height) {
for(int y = 1; y < height+1; y++) {
for(int x = 1; x < width+1; x++) {
if(board[y][x] == DEAD)
printf(" - ");
else
printf(" # ");
}
printf("\n");
}
}
It outputs as expected:它按预期输出:
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
memory freed successfully
And I free the memory I allocated in the create_game_board
function using this function:我使用这个函数释放我在create_game_board
函数中分配的内存:
void free_array_memory(int *arr[]) {
for(int i = 0; i < sizeof(*arr)/sizeof(arr[0]); i++)
free(arr[i]);
}
My question is in regards to my first try at implementing the create_game_board
function, which looked like this:我的问题是关于我第一次尝试实现create_game_board
函数,它看起来像这样:
// This does not work.
void create_game_board(int* dest[], int width, int height) {
for(int i = 0; i < height; i++) {
int row[width];
memset(dest, 0, width*sizeof(int));
dest[i] = row;
}
}
Below is what outputted to the console when I used this version of the function, in addition to the message I get when trying to free memory (width and height are the same values).下面是我使用这个版本的函数时输出到控制台的内容,以及我在尝试释放内存时得到的消息(宽度和高度是相同的值)。 I would've expected it to work like the one I mentioned at the beginning.我原以为它会像我一开始提到的那样工作。 Why doesn't this function work?为什么这个功能不起作用?
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
- - - - - - -
free(): invalid pointer
exited, aborted
In your earlier implementation, you set the pointers in src
to the address of the first element of row
.在您之前的实现中,您将src
的指针设置为row
的第一个元素的地址。 But - row
is a variable on the stack, not on the heap;但是 - row
是堆栈上的变量,而不是堆上的变量; it gets "de-allocated" when the stack is popped (and used by other functions as part of the stack area).当堆栈被弹出(并被其他函数用作堆栈区域的一部分)时,它会被“解除分配”。 You can't free()
that address.你不能free()
那个地址。
However, both implementations are problematic:但是,这两种实现都有问题:
create_game_board()
or something.您可以调用函数create_game_board()
或其他东西。height
different allocations.没有充分的理由对height
不同的分配。 Just allocate width*height
elements once, and set src
elements to point into different points in that region.只需分配一次width*height
元素,并将src
元素设置为指向该区域的不同点。src
is a poor choice of name for the game board row pointers parameter. src
是游戏板行指针参数的糟糕名称选择。 It's not the "source" of anything.它不是任何事物的“来源”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.