条件跳转或移动取决于未初始化的值

发布于 2024-11-30 03:37:39 字数 2458 浏览 1 评论 0原文

我已经为这个问题苦苦挣扎了一段时间,寻找每一种可能的解决方案。我是C语言新手,所以不太难。我知道我有一些未初始化的变量,但我找不到它们。我正在尝试打印一个矩阵。这是构造函数:

BoardP createNewBoard(int width, int high)
{  

    BoardP board = (BoardP) malloc(sizeof(Board));

    if (board == NULL)
    {
        reportError(MEM_OUT);
        return NULL;
    }
    board->height = high;
    board->width = width;
    board->board = (char**) malloc(high * sizeof(char*));
    int i;
    for (i=0; i<high; i++)
    {
        board->board[i] = (char*) malloc(width * sizeof(char));
        if (board->board[i] == NULL)
        {
            freeTempBoard(board,i);
            return NULL;
        }
    }

    return board;
}

构造函数返回 BoardP,Board 的打印器,即:

typedef struct Board
{
    int width;
    int height;
    char **board;
} Board;

现在我无法尝试打印 board->board。我循环遍历矩阵,并为每个单元格调用此函数:

static void printChar(ConstBoardP board, int X, int Y)
{
    if (X>=board->height || Y>=board->width)
    {
        printf(" ");
    }
    else
    {
        printf("%c ",board->board[X][Y]); //!!THIS IS LINE 299 IN Board.c!!
    }
}

最后,这是我得到的错误:

==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E973D9: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:880)
==4931==    by 0x4E6F01B: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)
==4931== 
==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E97401: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:887)
==4931==    by 0x4E6F01B: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)
==4931== 
==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E6F025: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)

现在有另一个文件调用 createNewBoard,然后创建 printBoard(newBoard,0,0)。唯一可能未初始化的是 board->board,除此之外我没有任何想法。我不知道如何调试它。 我知道它有很多文字,但我找不到问题。任何想法将不胜感激

I've been struggling with this problem for a while now, searching every possible solution. I'm new to C so I'ts hard. I know I have some variable uninitialized, but I can't find them. I am trying to print a matrix. Here's the constructor:

BoardP createNewBoard(int width, int high)
{  

    BoardP board = (BoardP) malloc(sizeof(Board));

    if (board == NULL)
    {
        reportError(MEM_OUT);
        return NULL;
    }
    board->height = high;
    board->width = width;
    board->board = (char**) malloc(high * sizeof(char*));
    int i;
    for (i=0; i<high; i++)
    {
        board->board[i] = (char*) malloc(width * sizeof(char));
        if (board->board[i] == NULL)
        {
            freeTempBoard(board,i);
            return NULL;
        }
    }

    return board;
}

The constructor returns BoardP, a pinter to Board, which is:

typedef struct Board
{
    int width;
    int height;
    char **board;
} Board;

Now I fail trying to print the board->board. I loop over the matrix and for each cell I call this function:

static void printChar(ConstBoardP board, int X, int Y)
{
    if (X>=board->height || Y>=board->width)
    {
        printf(" ");
    }
    else
    {
        printf("%c ",board->board[X][Y]); //!!THIS IS LINE 299 IN Board.c!!
    }
}

And fianlly here's the error I get:

==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E973D9: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:880)
==4931==    by 0x4E6F01B: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)
==4931== 
==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E97401: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:887)
==4931==    by 0x4E6F01B: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)
==4931== 
==4931== Conditional jump or move depends on uninitialised value(s)
==4931==    at 0x4E6F025: vfprintf (vfprintf.c:1614)
==4931==    by 0x4E75879: printf (printf.c:35)
==4931==    by 0x400D91: printChar (Board.c:299)
==4931==    by 0x400CED: printBoard (Board.c:284)
==4931==    by 0x400F1A: main (PlayBoard.c:19)

Now there's another file that calls createNewBoard, and then create printBoard(newBoard,0,0). The only thing that could possibly be uninitialized is board->board, other then that I have no ideas. I don't know how to debug it.
I know its a lot of text, but I can't find the problem. Any idea would be much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-12-07 03:37:39

尝试:

for (i=0; i<high; i++)
{
    board->board[i] = (char*) malloc(width * sizeof(char));
    /* ... */
    memset(board[i], 0, width);
}

Try:

for (i=0; i<high; i++)
{
    board->board[i] = (char*) malloc(width * sizeof(char));
    /* ... */
    memset(board[i], 0, width);
}
时光病人 2024-12-07 03:37:39

malloc 不会初始化内存,因此:

printf("%c ",board->board[X][Y]);

可能未初始化。使用 calloc 代替或在后面添加 memset

board->board[i] = (char*) malloc(width * sizeof(char));

malloc does not initialize the memory, so this:

printf("%c ",board->board[X][Y]);

might be uninitialized. Use calloc instead or add memset after

board->board[i] = (char*) malloc(width * sizeof(char));
素年丶 2024-12-07 03:37:39

在这一行:

board->board[i] = (char*) malloc(width * sizeof(char));

...您为板行分配空间,但您从未真正初始化该数组中的值。使用 memset(board[i], 0, width) 或显式循环数组来设置值。

(此外,作为一种风格问题,您不需要 强制转换 malloc的返回值。)

At the line:

board->board[i] = (char*) malloc(width * sizeof(char));

...you allocate space for the board row, but you never actually initialise the values in this array. Use memset(board[i], 0, width) or explicitly loop over the array to set the values.

(Also, as a matter of style, you don't need to cast malloc's return value.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文