c - 将文件读入 double int 指针

标签 c segmentation-fault pointer-to-pointer

我正在尝试将文件读入数组,但出现段错误,我知道我没有正确分配内存。我在这里做错了什么?我可以使用 read()while 循环。

编辑

我的完整功能,在我分成两部分之前

int     **ft_create_map(char *filename, int nb_cols, int nb_rows)
{
    int     **map;
    int     fd;
    int     row;
    int     col;
    ssize_t size;
    char    buf[BUF_SIZE];

    row = 0;
    size = 0;
    col = 0;
    map = (int **)malloc(nb_rows * sizeof(int *));
    if (!map)
            return (map);
            map[0] = malloc(sizeof(int) * nb_cols);
            fd = open(filename, O_RDONLY);
            if (!fd)
                    return (NULL);
                    while ((size = read(fd, buf, BUF_SIZE)))
                    {
                            if (size < 1)
                                    return (NULL);
                            buf[size] = '\0';
                            if (buf[0] == '\n')
                            {
                                    row += 1;
                                    col = 0;
                                    map[row] = malloc(sizeof(int) * nb_cols);
                            }
                            else
                            {
                                    if (buf[0] == '.')
                                            map[row][col] = 1;
                                    else if (buf[0] == 'o')
                                            map[row][col] = 0;
                                    col++;
                            }
                    }
    return (map);
 }

这里我尝试将前面的函数拆分为两个函数,因为我的函数需要少于 25 行代码。

void fill_map(int **map,int fd, int row, int col)
{
    ssize_t size;
    char    buf[BUF_SIZE];

    size = 0;
    while ((size = read(fd, buf, BUF_SIZE)))
    {
        if (size < 1)
           // return (0); commented out for testing
        buf[size] = '\0';
        if (buf[0] == '\n')
        {
            //this was the problem, allocating memory to map[0] twice.
            row += 1;
            map[row] = malloc(sizeof(int) * (col + 1));
            col = 0;
        }
        else
       {
           if (buf[0] == '.')
              map[row][col] = 1;
           else if (buf[0] == 'o')
              map[row][col] = 0;
           col++;
       }
    }
 }

 int        **ft_create_map(char *filename, int nb_cols, int nb_rows)
 {
    int     **map;
    int     fd;
    int     row;
    int     col;
    ssize_t size;
    // char buf[BUF_SIZE];

    row = 0;
    size = 0;
    col = 0;
    map = (int **)malloc(nb_rows * sizeof(int *));
    if (!map)
       return (map);
    map[0] = malloc(sizeof(int) * nb_cols);
    fd = open(filename, O_RDONLY);
    if (!fd)
       return (NULL);
    fill_map(map, fd, row, col);
    return (map);
 }

在我的主目录

int cols = count_cols(argv[1]);
int rows = count_rows(argv[1]);
int **arr;
// int  j;
// int i;

arr = ft_create_map(argv[1], cols, rows);
printf_max_square(arr, rows, cols);

code ideone

最佳答案

假设此行在

中被注释
 // return (0); commented out for testing

read()返回BUF_SIZE时,这一行

buf[size] = '\0';

写入到 buf 的范围之外,从而调用未定义的行为。

关于c - 将文件读入 double int 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36746524/

相关文章:

c - 通用链表

我可以在 Linux 驱动程序中使用 C11 的 <stdatomic.h> 吗?还是必须使用内存屏障的 Linux 函数?

c++ - OS X 上的 std::locale 段错误,无法在任何其他平台上重现

C++ 内存分配和重写 vector 类

c - 使用双指针返回指针的值

c - C 中的队列结构。通过取消引用指向该结构的指针来访问该结构中的指针

c++ - 覆盖 malloc 的问题

c - 如何使用c在char中查找大写字母的单词?

c - 取消引用自定义内存地址时出现段错误 (C)

c - 访问c中的指针数组