-1

ファイルを配列に読み込もうとしていますが、セグメンテーション違反が発生しています。メモリを正しく割り当てていないことがわかっています。ここで何が間違っていますか? read()使用してwhileループすることが許可されています。

編集

2 つの部分に分割する前の完全な機能

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

ここでは、前の関数を 2 つの関数に分割しようとしています。これは、関数に必要なコードが 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);

コードイデオン

4

2 に答える 2

3

この行がコメントされると仮定すると

 // return (0); commented out for testing

read()帰ってきた瞬間BUF_SIZE、このセリフ

buf[size] = '\0';

の範囲外に書き込みbuf、これにより未定義の動作が呼び出されます。

于 2016-04-20T14:29:44.157 に答える