将文件读入双int指针(Reading file into double int pointer)

编程入门 行业动态 更新时间:2024-10-19 08:51:49
将文件读入双int指针(Reading file into double int 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);

代码构思

I'm trying to read a file into an array but I'm getting segmentation fault, I know I'm not allocating memory correctly. What am I doing wrong here?. I'm allowed to use read() and while loop.

EDIT

my complete function, before I split in into two parts

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); }

Here I'm trying to split the previous function into two functions, because my functions needs to have less than 25 lines of code.

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); }

in my main

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

最满意答案

看起来你有潜在的内存泄漏。 在ft_create_map you have map[0] = malloc(sizeof(int) * nb_cols); 然后在fill_map中你有map[row] = malloc(sizeof(int) * (col + 1)); 其中row==0 。 因此,如果buf[0] == '\n'这将是一个mem泄漏。

实际上有点惊讶地听到(来自OP)这也导致了段错误,但如果我们知道确切的程序流程,这可能会变得很明显。

It looks like you have a potential memory leak. In ft_create_map you have map[0] = malloc(sizeof(int) * nb_cols); Then in fill_map you have map[row] = malloc(sizeof(int) * (col + 1)); where row==0. So this will be a mem leak if buf[0] == '\n'.

Actually kind of surprised to hear (from the OP) that this is also causing a segfault, but maybe that would become obvious if we knew the exact program flow.

更多推荐

本文发布于:2023-04-29 11:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   文件   int   Reading   double

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!