“未初始化值的使用”尽管有memset

发布于 2024-09-05 18:27:10 字数 2016 浏览 7 评论 0原文

我分配一个二维数组并使用 memset 用零填充它。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main() {
    int m=10;
    int n =10;
    int **array_2d;
    array_2d = (int**) malloc(m*sizeof(int*));
    if(array_2d==NULL) {
        printf("\n Could not malloc 2d array \n");
        exit(1);
    }
    for(int i=0;i<m;i++) {
        ((array_2d)[i])=malloc(n*sizeof(int));
        memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));
    }


    for(int i=0; i<10;i++){
        for(int j=0; j<10;j++){
            printf("(%i,%i)=",i,j);
            fflush(stdout);
            printf("%i ", array_2d[i][j]);
        }
        printf("\n");
    }
}

之后我使用 valgrind [1] 检查内存错误。我收到以下错误:第 24 行的条件跳转或移动取决于未初始化的值 (printf("%i ", array_2d[i][j]); )。我一直以为memset是初始化数组的函数。我怎样才能摆脱这个错误?

谢谢!

Valgrind 输出:

==3485== Memcheck, a memory error detector
==3485== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3485== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
==3485== Command: ./a.out
==3485== 
(0,0)=0 (0,1)===3485== Use of uninitialised value of size 4
==3485==    at 0x409E186: _itoa_word (_itoa.c:195)
==3485==    by 0x40A1AD1: vfprintf (vfprintf.c:1613)
==3485==    by 0x40A8FFF: printf (printf.c:35)
==3485==    by 0x8048724: main (playing_with_valgrind.c:39)
==3485== 
==3485== 
==3485== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- 
==3485== Conditional jump or move depends on uninitialised value(s)
==3485==    at 0x409E18E: _itoa_word (_itoa.c:195)
==3485==    by 0x40A1AD1: vfprintf (vfprintf.c:1613)
==3485==    by 0x40A8FFF: printf (printf.c:35)
==3485==    by 0x8048724: main (playing_with_valgrind.c:39)

[1] valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --db-attach=yes ./a.out

[gcc-cmd] gcc -std=c99 -lm -Wall -g3playing_with_valgrind.c

I allocate a 2d array and use memset to fill it with zeros.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main() {
    int m=10;
    int n =10;
    int **array_2d;
    array_2d = (int**) malloc(m*sizeof(int*));
    if(array_2d==NULL) {
        printf("\n Could not malloc 2d array \n");
        exit(1);
    }
    for(int i=0;i<m;i++) {
        ((array_2d)[i])=malloc(n*sizeof(int));
        memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));
    }


    for(int i=0; i<10;i++){
        for(int j=0; j<10;j++){
            printf("(%i,%i)=",i,j);
            fflush(stdout);
            printf("%i ", array_2d[i][j]);
        }
        printf("\n");
    }
}

Afterwards I use valgrind [1] to check for memory errors. I get following error: Conditional jump or move depends on uninitialised value(s) for line 24 (printf("%i ", array_2d[i][j]);). I always thought memset is the function to initialize arrays. How can I get rid off this error?

Thanks!

Valgrind output:

==3485== Memcheck, a memory error detector
==3485== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3485== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
==3485== Command: ./a.out
==3485== 
(0,0)=0 (0,1)===3485== Use of uninitialised value of size 4
==3485==    at 0x409E186: _itoa_word (_itoa.c:195)
==3485==    by 0x40A1AD1: vfprintf (vfprintf.c:1613)
==3485==    by 0x40A8FFF: printf (printf.c:35)
==3485==    by 0x8048724: main (playing_with_valgrind.c:39)
==3485== 
==3485== 
==3485== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- 
==3485== Conditional jump or move depends on uninitialised value(s)
==3485==    at 0x409E18E: _itoa_word (_itoa.c:195)
==3485==    by 0x40A1AD1: vfprintf (vfprintf.c:1613)
==3485==    by 0x40A8FFF: printf (printf.c:35)
==3485==    by 0x8048724: main (playing_with_valgrind.c:39)

[1] valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes --db-attach=yes ./a.out

[gcc-cmd] gcc -std=c99 -lm -Wall -g3 playing_with_valgrind.c

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

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

发布评论

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

评论(2

最美的太阳 2024-09-12 18:27:10

在这一行:

/* sizeof(n*sizeof(int)) retuns a value of type size_t.
   This means you are initializing only sizeof(size_t) of the array. */
memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));

应该是:

memset(((array_2d)[i]),0, n*sizeof(int));

At this line:

/* sizeof(n*sizeof(int)) retuns a value of type size_t.
   This means you are initializing only sizeof(size_t) of the array. */
memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));

It should be:

memset(((array_2d)[i]),0, n*sizeof(int));
↘人皮目录ツ 2024-09-12 18:27:10

将语句: 更改

memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));

为:

memset(((array_2d)[i]),0,n*sizeof(int));

您不应该在那里执行 sizeof 。它只会返回变量类型的大小,这不是您想要的。

Change the statement:

memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));

to:

memset(((array_2d)[i]),0,n*sizeof(int));

You should not be doing a sizeof there. It will just return the size of the type of the variable, which is not what you want.

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