我对 C 很陌生,我以前听说过缓冲区溢出,但我从未听说过堆栈缓冲区下溢。我一直在尝试阅读它,据我了解,我分配了太多内存?我只是想确保我正确理解了这个问题。所以我的问题与下面的代码有关,它需要几代人来更新康威生命游戏的给定文件。如果有人能解释我在哪里误解了某事,我将不胜感激。输入应遵循“./life.c #board.txt”的行,其中# 是代数,board.txt 是由“.”和“*”构成的板。board.txt 的第一行也包含行数和列数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void futureGens(int numRows, int numCols, int original[numRows][numCols], int generations){
int future[numRows][numCols];
int i, j;
for(i = 0; i < numRows; i++){
for(j = 0; j < numCols; j++){
int live = 0;
if(original[i-1][j-1] == 1){
live++;
}
if(original[i-1][j] == 1){
live++;
}
if(original[i-1][j+1] == 1){
live++;
}
if(original[i][j-1] == 1){
live++;
}
if(original[i][j] == 1){
live++;
}
if(original[i][j+1] == 1){
live++;
}
if(original[i+1][j-1] == 1){
live++;
}
if(original[i+1][j] == 1){
live++;
}
if(original[i+1][j+1] == 1){
live++;
}
live -= original[i][j];
switch(live){
case 0:
case 1:
future[i][j] = 0;
break;
case 2:
future[i][j] = original[i][j];
break;
case 3:
future[i][j] = 1;
break;
default:
future[i][j] = 0;
}
}
}
if(generations == 1){
//printf("\nFuture: \n");
for(i = 0; i < numRows; i++){
for(j = 0; j < numCols; j++){
if(future[i][j] == 1){
printf("*");
} else {
printf(".");
}
//printf("%d", future[i][j]);
}
printf("\n");
}
}
else {
futureGens(numRows, numCols, future, generations-1);
}
}
int main(int argc, char **argv){
if(argc != 3) {
return EXIT_FAILURE;
}
int generations = atoi(argv[1]);
FILE *fp = fopen(argv[2], "r");
if(fp == NULL){
printf("error: nothing in file\n");
return EXIT_FAILURE;
}
int numRows = 0, numCols = 0;
char line[256];
if(fgets(line, sizeof(line), fp)){
char c[256];
int p;
for(p = 0; p < 256; p++){
if(isdigit(line[p]) != 0){
c[p] = line[p];
} else {
break;
}
}
numRows = atoi(c);
numCols = atoi(c);
printf("row: %d, col: %d\n", numRows, numCols);
}
//initialize the original array
int original[numRows][numCols];
int i, j;
for(i = 0; i < numRows; i++){
fgets(line, sizeof(line), fp);
for(j = 0; j < numCols; j++){
char c = line[j];
if(c == '.'){
original[i][j] = 0;
} else if(c == '*'){
original[i][j] = 1;
}
}
}
futureGens(numRows, numCols, original, generations);
return EXIT_SUCCESS;
}