为重新分配的动态数组赋值时出现问题 - 迷宫求解器

问题描述 投票:0回答:1

我正在努力将值存储到动态数组,该数组是由 realloc() 函数重新分配的。

我正在创建迷宫解算器程序,作为内部表示,我想创建

tile_array
元素的一维数组
struct tile
,其中每个元素代表从输入文件读取的一个字符。
struct tile
存储字符和附加数据,例如数组、行和列(抽象)中的距离等。迷宫由
struct maze
表示,其中包含
tile array

我需要动态分配

tile_array
,因为输入文件的长度未知。在 while 循环的每次迭代中,程序都会创建一个
struct tile
并将其分配给一维数组的末尾。到目前为止一切正常。当数组已满时,它调用 realloc() 并继续将元素存储到数组末尾。这就是问题所在,我看不出原因。从这一点开始,printf() 函数为每个参数打印 NULL。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

struct tile
{
    // should be one element in tile array
    bool visited;
    char type;              // '#' / ' ' / 'X' / 'o'
    size_t distance;        // distance in tile_array
    size_t row;
    size_t col;
    struct tile* previous;  // pointer to previous tile
};

struct maze
{
    struct tile* tile_array;    // 1D array of <tile> structs
    size_t length;              // length of tile_array
    size_t *rows_lengths;          // [row1_len, row2_len, ...]
    size_t row_count;           // count of rows used
};

bool maze_create(struct maze *maze, FILE *file) { size_t 数组长度 = 4;

// allocate tile_array
struct tile *tile_array = malloc(array_length * sizeof(struct tile));
if (tile_array == NULL) {
    return false;
}

// allocate rows_lengths array
size_t rows_lengths_count = 4;
size_t *rows_lengths = malloc(rows_lengths_count * sizeof(size_t));
if (rows_lengths == NULL) {
    free(tile_array);
    tile_array = NULL;
    return false;
}

char curr_char;
size_t curr_distance = 0;
size_t curr_row = 0;
size_t curr_col = 0;
struct tile *previous = NULL;
while ((curr_char = fgetc(file)) != EOF) {
    assert(curr_distance <= array_length);
    assert(curr_row <= rows_lengths_count);

    // allocate more memory for tile_array
    if (curr_distance == array_length) {
        array_length += 4;
        struct tile *tmp = realloc(tile_array, (array_length * sizeof(struct tile)));
        if (tmp == NULL) {
            free(tile_array);
            tile_array = NULL;
            free(rows_lengths);
            rows_lengths = NULL;
            return false;
        }
        tile_array = tmp;
        tmp = NULL;
    }

    // allocate more memory for rows_lengths
    if (rows_lengths_count == curr_row) {
        rows_lengths_count += 4;
        size_t *tmp = realloc(rows_lengths, (rows_lengths_count * sizeof(size_t)));
        if (tmp == NULL) {
            free(tile_array);
            tile_array = NULL;
            free(rows_lengths);
            rows_lengths = NULL;
            return false;
        }
        rows_lengths = tmp;
        tmp = NULL;
    }

    if (curr_char == '\n') {
    // set variables & continue
    }
    if (illegal_input_char(curr_char)) {
    // invalid input, free memory & return false
    }

    // create new tile and insert to the maze
    struct tile curr_tile = {false, curr_char, curr_distance, curr_row, curr_col, previous};
    tile_array[curr_distance] = curr_tile;

    printf("WRITTEN CHAR: '%lu', direction:%2lu", ((*maze).tile_array[curr_distance]).type, ((*maze).tile_array[curr_distance]).distance);

    curr_col++;
    curr_distance++;
    previous = &curr_tile;
}

(*maze).length = curr_distance;
(*maze).row_count = curr_row - 1;
return true;
arrays c dynamic dynamic-memory-allocation maze
1个回答
0
投票

从我在代码中看到的,您的tile_array和rows_lengths只是局部变量,它们与Maze的tile_array和rows_lengths成员不同。但是,当您打印它们时,您引用了 Maze 的成员,因此输出为空。您可能想在任何地方引用 (*maze).tile_array 和 (*maze).rows_lengths 。希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.