2012-04-22 1 views
0

txt 파일에서 단어를 선택하는 프로그램을 작성하고 있습니다. 플래그 컴파일 : -std = gnu99 프로그램에 GDB 및 Valgrind를 사용하여 디버깅 중 일부 세그먼트 오류가 있습니다. Valgrind의 플래그 : --track-기원 = 예 --leak 검사 = 전체 --show-도달 = 예 나는 모든 여기의Valgrind 조건부 점프 또는 이동은 초기화되지 않은 값에 따라 달라집니다.

먼저 코드가

    설명 Valgrind의 오류 메시지에 대한 몇 가지 질문이 있습니다
  • 구조체 문자 : 이 특정 문자 (예 : a 또는 b 또는 c, ecc)로 시작하는 모든 단어를 포함하는 구조체입니다.
  • include 함수 : 단어를 특정 구조에 추가해야 할 때 insert 함수는 단어 배열을 검색하여이 단어의 출현 여부를 확인합니다. 그렇다면 단어 수가 증가합니다. 그렇지 않으면 배열에 단어가 추가됩니다. 공백이 없으면 함수는 배열 공간을 늘립니다.
  • 메인은 조용한 선형입니다. 당신은 알파벳의 각 문자에 대한 구조체를 볼 수 있습니다, 당신은 텍스트 파일의 라인을 읽는 동안 루프를 볼 수 있습니다. 초기화 값에 따라 조건부 점프에 대한
  • 여기


#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의 오류 메시지 :

[[email protected] 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를 초기화했습니다. 당신은 당신이 calloc를 사용해야하는 경우 malloc를 사용하여 연

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)); 

등에 내가 무엇을 볼 수 있습니다에서

+4

진심으로 ?? 450 줄의 코드와 240 줄의 오류 메시지? 최소한의 예로 줄일 수 없었습니까? – huon

+2

문제의 범위를 좁혀주십시오. 게시 한 코드의 약 5 %로 재현 할 수 있어야하며 그렇게하는 동안 오류가 발생할 수 있습니다. – Mat

+4

각 문자에 대해 struct 변수 이름이 분리됩니다 ...?! –

답변

7
character->words = realloc(character->words, (character->arrayCounter + 1) * sizeof(char*)); 
character->count = realloc(character->count, (character->arrayCounter + 1) * sizeof(int)); 
character->arrayCounter++; 
  1. 당신은 할 수 없습니다 : 다시 말하지만, 그들은 당신이 알아야 할 당신에게 더 이상 도달하지 마십시오.
  2. 크기를 하나의 요소 만 늘리지 마십시오. 비효율적이며 비용이 많이 듭니다.메모리 조각화로 이어지기 때문에 더 많은 양의 메모리를 사용하는 것보다 적은 메모리를 사용하지는 않을 것입니다. 예를 들어, 크기를 두 배로 늘리고 크기를 1.5로 곱하십시오. 증가]).
  3. character->words의 새 저장소에 NULL이 반드시 포함될 필요는 없으며 원래 words 포인터 : a.words = malloc((a.arrayCounter)*sizeof(char*));처럼 초기화되지 않았습니다.

아마 3. Valgrind의 보고서 것입니다.

문자 뒤에 이름이 지정된 character 구조 대신에 하나의 배열이 있고 그 배열에 색인이 지정되어 있으면 코드가 더 짧고 읽기 쉽습니다. struct character e;character_array['e' - 'a'];에 해당합니다.

4

. 그런 다음 나중에 내용을 초기화하지 않고 a.words의 내용을 사용합니다.

편집 : 또한 코드를 정리하면 실제로 처리 할 수 ​​있습니다. 모든 구조와 혼란은 쉽게 두 개의 해시 테이블로 대체 될 수 있습니다.

1

valgrind가보고 한 문제 (다른 것들은 건드릴 수 없음)는 mallocrealloc입니다. 이것들은 메모리를 0으로 초기화하지 않고 단지 calloc 만 수행합니다. 당신이 그를 고칠 때, realloc에 의한 오류가 남아있는 사람이 될 것입니다,

==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 

을 그리고 : Valgrind의의 메시지는 예를 들어, 당신이 말해. 당신은 결코ptr = realloc(ptr,size);realloc하면 반환 NULL은, 메모리가 ptr가 유출 가리키는 구조를 사용해야합니다

==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)