-1

我正在编写一个程序来从 txt 文件中选择单词。编译标志:-std=gnu99 该程序有一些分段错误,我正在使用 GDB 和 Valgrind 进行调试。Valgrind 标志: --track-origins=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。

4

3 回答 3

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);If reallocreturns NULL,指向的内存ptr被泄露,你无法再访问它。
  2. 不要只增加一个元素的大小,这是低效且昂贵的。由于它也会导致内存碎片,因此它可能不会使用更少的内存而不是增加更大的数量,比如增加一个常数因子(将大小加倍,乘以 1.5 [并加一以确保大小确实增加])。
  3. 中的新存储character->words不一定包含NULL,它是未初始化的,原始words指针也是如此:a.words = malloc((a.arrayCounter)*sizeof(char*));

可能 3. 是 valgrind 报告的。

如果你有一个数组并索引到该数组中,而不是有一个以字符命名的结构,那么你的代码会短,更易读,所以 eg对应于.characterstruct character e;character_array['e' - 'a'];

于 2012-04-22T11:43:08.130 回答
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而无需先初始化内容。

编辑:另外,你的代码真的可以做一些清理工作。你所有的结构和混乱都可以很容易地用两个哈希表代替。

于 2012-04-22T11:33:24.727 回答
1

valgrind 报告的问题(我不会涉及其他问题)是使用mallocand realloc。这些不会将它们的内存初始化为 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) 
于 2012-04-22T11:42:02.170 回答