0

我的程序是这样的:

char *line = "hello a b c d e f g ";
/* split it into words */
char **argv = NULL;
**argv = malloc(sizeof(char*));
if (*argv = NULL)
    printf("null malloc");
    printf("malloc ok");
    int i;
    for (i=0;;i++){
        printf("i=%d", i);
        line = strwrd(line, argv[i], 1024, " \t");
        printf("strwrk ok");
        if (argv[i] = NULL)
             break;
        **argv = realloc(*argv, (i+2)*sizeof(char*));   
        printf("realloc ok");
    }
}

当我在 valgrind 中运行它时,它说:

==22169== Invalid read of size 4
==22169==    at 0x804858B: main (printTest.c:27)
==22169==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

因为我是 C 的菜鸟,所以我不知道 malloc 是否有问题。

4

1 回答 1

2
**argv = malloc(sizeof(char*));

应该

argv = malloc(sizeof(*argv));
于 2012-09-16T19:32:59.480 回答