Valgrind条件跳跃或移动取决于未初始化的值(s)

时间:2012-04-22 11:25:40

标签: c gdb valgrind

我正在编写一个程序来从txt文件中选择单词。 编译标志:-std = gnu99 该程序有一些分段错误,我正在使用GDB和Valgrind进行调试。 Valgrind标志: - track-origin = yes --leak-check = full --show-reachable = yes 我对Valgrind错误消息有一些疑问

首先,这里的代码解释了

  • struct character:是包含所有单词的结构 以特定字母开头,例如a或b或c,ecc。
  • include函数:当你必须在特定结构中添加一个单词时,insert函数会扫描单词数组以查看该单词是否出现。如果是,则增加字数。否则它会将单词添加到数组中。如果没有空格,则该函数会增加数组空间。
  • 主要是安静的线性。你可以看到字母表中每个字符的结构,你可以看到一个用于读取文本文件行的while循环。


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>


struct character
{
    char **words;
    int *count;
    int arrayCounter;
};

/*
 * if finds in the array the word to add, then increase counter
 * if it does not find the word, it puts the word non la trova ce la mette nel primo slot che è NULL, quindi vuoto.
 * se non trova slot vuoti aumenta la dimensione degli array e poi torna al punto due.
 */
void insert(struct character *character, char *word)
{
    while(1){
    printf("inserting word %s\n", word);
    for(int i = 0; i < character->arrayCounter; i++)
    {
        printf("%d\n",i);
        if((character->words[i] != NULL) && (strcmp(character->words[i], word) == 0))
        {
            printf("Insert: I found a corrispondence of word in the array, now I increase the word counter in the struct\n");
            printf("Original word: %s word to compare %s\n", character->words[i], word);

            character->count[i] = character->count[i] + 1;
            printf("Word %s counter: %d\n", character->words[i], character->count[i]);
            return;
        }

    }

    ciclo:
    for(int i = 0; i < character->arrayCounter; i++)
    {
        printf("ciclo: %d\n", i);
        if(character->words[i] == NULL)
        {
            printf("Insert: ho trovato un posto vuoto nello slot %d e ci metto la word %s\n", i, word);
            character->words[i] = malloc((strlen(word) + 1) * sizeof(char));
            strcpy(character->words[i], word);
            character->count[i] = character->count[i] + 1;
            printf("Insert: controllo, la word è %s, il suo contatore è %d\n", character->words[i], character->count[i]);

            printf("\n\n\n\n");
            return;
        }
    }

    printf("!!!Increasing arrayCounter dimension, now it is %d\n", character->arrayCounter);
    character->words = realloc(character->words, (character->arrayCounter + 1) * sizeof(char*));
    character->count = realloc(character->count, (character->arrayCounter + 1) * sizeof(int));
    character->arrayCounter++;
    printf("!!!Dimension increase, arrayCounter is %d\n", character->arrayCounter);
    /*goto ciclo;*/}
}

int main()
{
    FILE *fileToRead;
    if((fileToRead = fopen("/home/caterpillar/Universita/workspace_Progetti/SO_ricercaParole/testo2.txt", "r")) == NULL)
    {
        printf("error in opening file\n" "%s\n", strerror(errno));
        exit(EXIT_FAILURE);

    }

    struct character a;
    memset(&a, 0, sizeof(a));
    a.arrayCounter = 1;
    a.words = malloc((a.arrayCounter)*sizeof(char*));
    a.count = calloc(a.arrayCounter,sizeof(int));

    struct character c;
    memset(&c, 0, sizeof(c));
    c.arrayCounter = 1;
    c.words = malloc((c.arrayCounter)*sizeof(char*));
    c.count = calloc(c.arrayCounter,sizeof(int));

    struct character e;
    memset(&e, 0, sizeof(e));
    e.arrayCounter = 1;
    e.words = malloc((e.arrayCounter)*sizeof(char*));
    e.count = calloc(e.arrayCounter,sizeof(int));

    struct character g;
    memset(&g, 0, sizeof(g));
    g.arrayCounter = 1;
    g.words = malloc((g.arrayCounter)*sizeof(char*));
    g.count = calloc(g.arrayCounter,sizeof(int));

    struct character i;
    memset(&i, 0, sizeof(i));
    i.arrayCounter = 1;
    i.words = malloc((i.arrayCounter)*sizeof(char*));
    i.count = calloc(i.arrayCounter,sizeof(int));

    struct character m;
    memset(&m, 0, sizeof(m));
    m.arrayCounter = 1;
    m.words = malloc((m.arrayCounter)*sizeof(char*));
    m.count = calloc(m.arrayCounter,sizeof(int));

    struct character o;
    memset(&o, 0, sizeof(o));
    o.arrayCounter = 1;
    o.words = malloc((o.arrayCounter)*sizeof(char*));
    o.count = calloc(o.arrayCounter,sizeof(int));

    struct character q;
    memset(&q, 0, sizeof(q));
    q.arrayCounter = 1;
    q.words = malloc((q.arrayCounter)*sizeof(char*));
    q.count = calloc(q.arrayCounter,sizeof(int));

    struct character s;
    memset(&s, 0, sizeof(s));
    s.arrayCounter = 1;
    s.words = malloc((s.arrayCounter)*sizeof(char*));
    s.count = calloc(s.arrayCounter,sizeof(int));

    struct character u;
    memset(&u, 0, sizeof(u));
    u.arrayCounter = 1;
    u.words = malloc((u.arrayCounter)*sizeof(char*));
    u.count = calloc(u.arrayCounter,sizeof(int));

    struct character z;
    memset(&z, 0, sizeof(z));
    z.arrayCounter = 1;
    z.words = malloc((z.arrayCounter)*sizeof(char*));
    z.count = calloc(z.arrayCounter,sizeof(int));





    struct character b;
    memset(&b, 0, sizeof(b));
    b.arrayCounter = 1;
    b.words = malloc((b.arrayCounter)*sizeof(char*));
    b.count = calloc(b.arrayCounter,sizeof(int));

    struct character d;
    memset(&d, 0, sizeof(d));
    d.arrayCounter = 1;
    d.words = malloc((d.arrayCounter)*sizeof(char*));
    d.count = calloc(d.arrayCounter,sizeof(int));

    struct character f;
    memset(&f, 0, sizeof(f));
    f.arrayCounter = 1;
    f.words = malloc((f.arrayCounter)*sizeof(char*));
    f.count = calloc(f.arrayCounter,sizeof(int));

    struct character h;
    memset(&h, 0, sizeof(h));
    h.arrayCounter = 1;
    h.words = malloc((h.arrayCounter)*sizeof(char*));
    h.count = calloc(h.arrayCounter,sizeof(int));

    struct character l;
    memset(&l, 0, sizeof(l));
    l.arrayCounter = 1;
    l.words = malloc((l.arrayCounter)*sizeof(char*));
    l.count = calloc(l.arrayCounter,sizeof(int));

    struct character n;
    memset(&n, 0, sizeof(n));
    n.arrayCounter = 1;
    n.words = malloc((n.arrayCounter)*sizeof(char*));
    n.count = calloc(n.arrayCounter,sizeof(int));

    struct character p;
    memset(&p, 0, sizeof(p));
    p.arrayCounter = 1;
    p.words = malloc((p.arrayCounter)*sizeof(char*));
    p.count = calloc(p.arrayCounter,sizeof(int));

    struct character r;
    memset(&r, 0, sizeof(r));
    r.arrayCounter = 1;
    r.words = malloc((r.arrayCounter)*sizeof(char*));
    r.count = calloc(r.arrayCounter,sizeof(int));

    struct character t;
    memset(&t, 0, sizeof(t));
    t.arrayCounter = 1;
    t.words = malloc((t.arrayCounter)*sizeof(char*));
    t.count = calloc(t.arrayCounter,sizeof(int));

    struct character v;
    memset(&v, 0, sizeof(v));
    v.arrayCounter = 1;
    v.words = malloc((v.arrayCounter)*sizeof(char*));
    v.count = calloc(v.arrayCounter,sizeof(int));




    char *line;
    line = malloc(5000*sizeof(char));
    ssize_t bytesRead = 0;
    ssize_t lineLength = 5000;
    //while(fgets(line, 5000, fileToRead) != (NULL))
    while((bytesRead = getline(&line, &lineLength, fileToRead)) != -1)
    {
        line[bytesRead + 1] = '\0';
        printf("linea è %s, strlen è %d\n", line, strlen(line));
        printf("tronco l'accapo\n");
        line[strlen(line) - 1] = '\0';
        printf("Nuova linea è %s, strlen è %d\n", line, strlen(line));
        char *buffer;
        buffer = strtok(line, " ");
        /*if(buffer =! NULL)
        {
            break;
        }*/
        if((buffer != NULL) && (buffer[0] == 'a'))
        {
            insert(&a, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'c'))
        {
            insert(&c, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'e'))
        {
            insert(&e, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'g'))
        {
            insert(&g, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'i'))
        {
            insert(&i, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'm'))
        {
            insert(&m, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'o'))
        {
            insert(&o, buffer);
        }
        if((buffer != NULL) && (buffer[0] == 'q'))
        {
            insert(&q, buffer);
        }
        if(((buffer != NULL) && buffer[0] == 's'))
        {
            insert(&s, buffer);
        }
        if(((buffer != NULL) && buffer[0] == 'u'))
        {
            insert(&u, buffer);
        }
        if(((buffer != NULL) && buffer[0] == 'z'))
        {
            insert(&z, buffer);
        }

        printf("***********************\n");
        for(int i = 0; (i < a.arrayCounter) && (a.words[i] != NULL); i++)
        {
            printf("Parola %s contatore %d\n", a.words[i], a.count[i]);
        }
        printf("***********************\n");
        //memset(buffer, NULL, 50*sizeof(char));
        while(1)
        {
            printf("while 1\n");
            buffer = strtok(NULL, " ");
            if(buffer == NULL)
            {
                printf("buffer is NULL, exiting from loop and going to next line\n");
                break;
            }

            if((buffer != NULL) && (buffer[0] == 'a'))
            {
                insert(&a, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'c'))
            {
                insert(&c, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'e'))
            {
                insert(&e, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'g'))
            {
                insert(&g, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'i'))
            {
                insert(&i, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'm'))
            {
                insert(&m, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'o'))
            {
                insert(&o, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'q'))
            {
                insert(&q, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 's'))
            {
                insert(&s, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'u'))
            {
                insert(&u, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'z'))
            {
                insert(&z, buffer);
            }



            if((buffer != NULL) && (buffer[0] == 'b'))
            {
                insert(&b, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'd'))
            {
                insert(&d, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'f'))
            {
                insert(&f, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'h'))
            {
                insert(&h, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'l'))
            {
                insert(&l, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'n'))
            {
                insert(&n, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'p'))
            {
                insert(&p, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'r'))
            {
                insert(&r, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 't'))
            {
                insert(&t, buffer);
            }
            if((buffer != NULL) && (buffer[0] == 'v'))
            {
                insert(&v, buffer);
            }







            //memset(buffer, NULL, 50*sizeof(char));
        }
    }
    printf("***********************\n");
    for(int ii = 0; (ii < c.arrayCounter) && (c.words[ii] != NULL); ii++)
    {
        printf("Word %s count %d\n", c.words[ii], c.count[ii]);
    }
    printf("***********************\n");

    printf("***********************\n");
    for(int ii = 0; (ii < e.arrayCounter) && (e.words[ii] != NULL); ii++)
    {
        printf("Word %s count %d\n", e.words[ii], e.count[ii]);
    }
    printf("***********************\n");
    /*
    for(int i = 0; i < 21; i++)
    {
        printf("!!!!!!!STAMPO PAROLE CHE INIZIANO CON LETTERA %s\n", arrayCaratteri[i]);
        printf("***********************\n");
        for(int ii = 0; (ii < arrayCaratteri[i].arrayCounter) && (arrayCaratteri[i].words[ii] != NULL); ii++)
        {
            printf("Parola %s contatore %d\n", arrayCaratteri[i].words[ii], arrayCaratteri[i].count[ii]);
        }
        printf("***********************\n");
    }*/
    fclose(fileToRead);  // close the file prior to exiting the routine
    free(a.words);
    free(a.count);
    free(b.words);
    free(b.count);
    free(c.words);
    free(c.count);
    free(d.words);
    free(d.count);
    free(e.words);
    free(e.count);
    free(f.words);
    free(f.count);
    free(g.words);
    free(g.count);
    free(h.words);
    free(h.count);
    free(i.words);
    free(i.count);
    free(l.words);
    free(l.count);
    free(m.words);
    free(m.count);
    free(n.words);
    free(n.count);
    free(o.words);
    free(o.count);
    free(p.words);
    free(p.count);
    free(q.words);
    free(q.count);
    free(r.words);
    free(r.count);
    free(s.words);
    free(s.count);
    free(t.words);
    free(t.count);
    free(u.words);
    free(u.count);
    free(v.words);
    free(v.count);
    free(z.words);
    free(z.count);

    printf("terminate successfully\n");
    return 0;
}

这里有一些关于条件跳转的Valgrind错误消息,取决于初始值:

[caterpillar@hostname Debug]$ valgrind --track-origins=yes 
--leak-check=full ./SO_ricercaParole 
==2791== Memcheck, a memory error detector 
==2791== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. 
==2791== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info 
==2791== Command: ./SO_ricercaParole 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x8049520: main (parole.c:325) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048B17: main (parole.c:119) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x8049520: main (parole.c:325) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048B17: main (parole.c:119) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x80496E2: main (parole.c:364) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048E5C: main (parole.c:177) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80496E2: main (parole.c:364) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048E5C: main (parole.c:177) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048A5D: main (parole.c:107) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048A5D: main (parole.c:107) 
==2791== 
==2791== Use of uninitialised value of size 4 
==2791== at 0x4D9851AB: _itoa_word (_itoa.c:195) 
==2791== by 0x4D98A03B: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D9851B3: _itoa_word (_itoa.c:195) 
==2791== by 0x4D98A03B: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791==
at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D98A3FD: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D989D7D: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80494BC: main (parole.c:317) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x804982B: main (parole.c:392) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x804907E: main (parole.c:219) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791==
by 0x804982B: main (parole.c:392) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x804907E: main (parole.c:219) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x8049584: main (parole.c:333) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048BD1: main (parole.c:131) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x8049584: main (parole.c:333) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048BD1: main (parole.c:131) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x804976F: main (parole.c:376) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048F52: main (parole.c:195) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x804976F: main (parole.c:376) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048F52: main (parole.c:195) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048FE8: main (parole.c:207) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048FE8: main (parole.c:207) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x80494EE: main (parole.c:321) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048ABA: main (parole.c:113) 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80494EE: main (parole.c:321) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048ABA: main (parole.c:113) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791==
by 0x80497FC: main (parole.c:388) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8049033: main (parole.c:213) 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80497FC: main (parole.c:388) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8049033: main (parole.c:213) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791==
by 0x80492E6: main (parole.c:267) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048B74: main (parole.c:125) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x80492E6: main (parole.c:267) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048B74: main (parole.c:125) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80486E1: insert (parole.c:38) 
==2791== by 0x804964C: main (parole.c:349) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048D45: main (parole.c:155) 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x80487E0: insert (parole.c:54) 
==2791== by 0x804964C: main (parole.c:349) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236) 
==2791== by 0x8048D45: main (parole.c:155) 
==2791== Use of uninitialised value of size 4 
==2791== at 0x4D9851AB: _itoa_word (_itoa.c:195) 
==2791== by 0x4D98A03B: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791==
at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D9851B3: _itoa_word (_itoa.c:195) 
==2791== by 0x4D98A03B: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791==
at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D98A3FD: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== 
==2791== Conditional jump or move depends on uninitialised value(s) 
==2791== at 0x4D989D7D: vfprintf (vfprintf.c:1570) 
==2791== by 0x4D98FF8E: printf (printf.c:35) 
==2791== by 0x80497CD: main (parole.c:384) 
==2791== Uninitialised value was created by a heap allocation 
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525) 
==2791== by 0x804892F: insert (parole.c:69) 
==2791== by 0x80497CD: main (parole.c:384) 

为什么会出现这种消息?我初始化了所有结构和数组,ecc.ecc。

3 个答案:

答案 0 :(得分:7)

character->words = realloc(character->words, (character->arrayCounter + 1) * sizeof(char*));
character->count = realloc(character->count, (character->arrayCounter + 1) * sizeof(int));
character->arrayCounter++;
  1. 你应该从不使用构造ptr = realloc(ptr,size);如果realloc返回NULL,则ptr指向的内存会被泄露,你可以'再到它了。
  2. 不要仅仅增加一个元素,这是低效且昂贵的。因为它也会导致内存碎片,它可能不会使用比增加更多内存更少的内存,比如增加一个常数因子(大小加倍,乘以1.5 [并添加一个以确保大小真的增加的])。
  3. character->words中的新存储空间不一定包含NULL,它与原始words指针一样未初始化:a.words = malloc((a.arrayCounter)*sizeof(char*));
  4. 可能3.是valgrind的报道。

    如果不是在字符后面加上一个character结构,那么你的代码将 更短且更具可读性,你有一个数组并被索引到该数组中,所以struct character e;将与character_array['e' - 'a'];对应。

答案 1 :(得分:4)

从我所看到的你就是在诗节中

struct character a;
memset(&a, 0, sizeof(a));
a.arrayCounter = 1;
a.words = malloc((a.arrayCounter)*sizeof(char*));
a.count = calloc(a.arrayCounter,sizeof(int));

等。在使用malloc时使用calloc。然后,您可以在不首先初始化内容的情况下使用a.words的内容。

修改:此外,您的代码可以真正完成一些清理工作。您可以使用两个哈希表轻松替换所有结构和混乱。

答案 2 :(得分:1)

valgrind报告的问题(我不会涉及其他问题)是使用mallocrealloc。这些不会将其内存初始化为0,只有calloc才会这样做。 Valgrind的消息告诉你,例如:

==2791== Conditional jump or move depends on uninitialised value(s)
==2791== at 0x80486E1: insert (parole.c:38)
==2791== by 0x8049520: main (parole.c:325)
==2791== Uninitialised value was created by a heap allocation  <<< TELLING YOU THE CAUSE
==2791== at 0x4007D89: malloc (vg_replace_malloc.c:236)
==2791== by 0x8048B17: main (parole.c:119)                  <<< LOCATION IN CODE

而且,当你修复它们时,由realloc引起的错误将是剩下的。他们再一次告诉你需要知道的事情:

==2791== Use of uninitialised value of size 4
==2791== at 0x4D9851AB: _itoa_word (_itoa.c:195)
==2791== by 0x4D98A03B: vfprintf (vfprintf.c:1570)
==2791== by 0x4D98FF8E: printf (printf.c:35)
==2791== by 0x80494BC: main (parole.c:317)
==2791== Uninitialised value was created by a heap allocation
==2791== at 0x4007E73: realloc (vg_replace_malloc.c:525)
==2791== by 0x804892F: insert (parole.c:69)                <<< LOCATION IN CODE
==2791== by 0x80494BC: main (parole.c:317)