C语言程序因二维数组值比较操作崩溃,请求技术支持
gol Struct's 2D Array Hey there, let's figure out why your program is crashing silently when you try to access board[1][1] in your gol struct. C crashes without error messages almost always boil down to invalid memory access—here are the most likely culprits and how to fix them:
1. You Haven't Properly Allocated Memory for the 2D Array
Your int **board is just a pointer-to-pointer—it doesn't point to any valid memory until you explicitly allocate it. Skipping this step means board[1][1] is trying to read from random, unowned memory, which triggers an immediate crash.
Here's how to correctly initialize the board when creating a gol instance:
#include <stdlib.h> // Function to create and initialize a gol struct gol* create_gol(size_t size) { gol* game = malloc(sizeof(gol)); if (!game) return NULL; // Handle malloc failure game->size = size; // Allocate array of row pointers game->board = malloc(size * sizeof(int*)); if (!game->board) { free(game); return NULL; } // Allocate each row's int array for (size_t i = 0; i < size; i++) { game->board[i] = malloc(size * sizeof(int)); if (!game->board[i]) { // Clean up already allocated memory if one malloc fails for (size_t j = 0; j < i; j++) { free(game->board[j]); } free(game->board); free(game); return NULL; } // Optional: Initialize all values to 0 to avoid garbage data for (size_t k = 0; k < size; k++) { game->board[i][k] = 0; } } return game; }
2. You're Accessing the Array Out of Bounds
Remember, C arrays are 0-indexed. If your gol struct's size is 1, then board[1][1] is way outside the valid range (only board[0][0] exists). Even if size is 2, double-check that you're not accidentally using indices equal to or larger than size.
Add a quick check before accessing the value to catch this:
// Example of safe access gol* my_game = create_gol(2); if (my_game) { size_t row = 1; size_t col = 1; if (row < my_game->size && col < my_game->size) { int value = my_game->board[row][col]; // Do your processing based on value (0 or 1) } else { // Handle out-of-bounds error printf("Error: Index (%zu, %zu) is out of bounds for size %zu\n", row, col, my_game->size); } }
3. Your gol Instance Isn't Properly Initialized
If you're declaring a local gol variable without initializing it, or using malloc but not setting board to NULL before allocation, board could be a wild pointer pointing to random memory.
Bad:
gol my_game; // Local struct, board is uninitialized garbage my_game.board[1][1] = 1; // Crash!
Good:
// Use the create_gol function we wrote earlier gol* my_game = create_gol(3); // Or if using a local struct, explicitly initialize: gol my_game = {.board = NULL, .size = 0};
Bonus: Debugging Tips
- Use a debugger like
gdbto run your program—it will tell you exactly where the crash happens, which helps narrow down the issue. - Print the
sizevalue right before accessing the array to confirm it's what you expect. - Check every
malloccall forNULL—if allocation fails, your program will behave unpredictably.
Don't forget to clean up memory when you're done to avoid leaks:
void destroy_gol(gol* game) { if (!game) return; for (size_t i = 0; i < game->size; i++) { free(game->board[i]); } free(game->board); free(game); }
内容的提问来源于stack exchange,提问作者Ben Leonforte

