2016-02-27 85 views
0

我是新成員,作爲此論壇。我的問題是關於兩個版本的gcc編譯器給我不同的結果。 gcc版本4.9.2編譯並運行時沒有核心轉儲。但是,海灣合作委員會版本5.3編譯沒有任何警告,但執行轉儲核心。如果有人能夠啓發我的不同,我將非常感激。我已經嘗試過用malloc,它通過兩個gcc版本導致更多的錯誤。提前致謝。請閱讀下面的更多細節。Valgrind - 所有分配的內存已釋放,仍然無效

有問題的版本

1. gcc版本4.9.2 20150212(紅帽4.9.2-6)(GCC)(Fedora的21 - 3.19.5-200.fc21.i686) - Valgrind的3.10

2. gcc版本5.3.1 20151207(紅帽5.3.1-2)(GCC)(Fedora的23 - 4.2.3-300.fc23.x86_64) - Valgrind的3.11

的代碼片段給出如下:

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


char *somval[12][2] = 
{ 
    {"one", "ONE"}, 
    {"two", "TWO"}, 
    {"three", "THREE"}, 
    {"four", "FOUR"}, 
    {"five", "FIVE"}, 
    {"six", "SIX"}, 
    {"seven", "SEVEN"}, 
    {"eight", "EIGHT"}, 
    {"nine", "NINE"}, 
    {"ten", "TEN"}, 
    {NULL, NULL} 
}; 

typedef struct _getval 
{ 
    char **val1; 
    char **val2; 
}GETVAL; 

int main() 
{ 
int i; 

GETVAL *getval; 
getval = calloc(1, sizeof(GETVAL)); 
getval->val1 = calloc(12, sizeof(int)); 
getval->val2 = calloc(12, sizeof(int)); 

for(i = 0; somval[i][1] != NULL; i++) 
    { 
    getval->val1[i] = calloc(101, sizeof(int)); 
    getval->val1[i] = somval[i][0]; 
    getval->val2[i] = somval[i][1]; 

    printf("%s = %s\n", getval->val1[i], getval->val2[i]); 
    } 

for(i = 0; somval[i][1] != NULL; i++) 
    { 
    free(getval->val1[i]); 
    } 
free(getval->val1); 
free(getval->val2); 
free(getval); 

return 0; 
} 

Compile the program with gcc or cc and valgrind ./a.out gives the following on gcc version 4.92. The program runs fine without a core dump and executed without valgrind. 

==4818== Memcheck, a memory error detector 
==4818== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 
==4818== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info 
==4818== Command: ./a.out 
==4818== 
one = ONE 
two = TWO 
three = THREE 
four = FOUR 
five = FIVE 
six = SIX 
seven = SEVEN 
eight = EIGHT 
nine = NINE 
ten = TEN 
==4818== Invalid free()/delete/delete[]/realloc() 
==4818== at 0x40082CD: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==4818== by 0x80485A1: main (in /users/netha/NTFiles/progs/cprog/test/a.out) 
==4818== Address 0x8048684 is not stack'd, malloc'd or (recently) free'd 
==4818== 
==4818== 
==4818== HEAP SUMMARY: 
==4818==  in use at exit: 4,040 bytes in 10 blocks 
==4818== total heap usage: 13 allocs, 13 frees, 4,144 bytes allocated 
==4818== 
==4818== LEAK SUMMARY: 
==4818== definitely lost: 4,040 bytes in 10 blocks 
==4818== indirectly lost: 0 bytes in 0 blocks 
==4818==  possibly lost: 0 bytes in 0 blocks 
==4818== still reachable: 0 bytes in 0 blocks 
==4818==   suppressed: 0 bytes in 0 blocks 
==4818== Rerun with --leak-check=full to see details of leaked memory 
==4818== 
==4818== For counts of detected and suppressed errors, rerun with: -v 
==4818== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 0 from 0) 

---------------------------------------------------- 
Compile the program with gcc or cc and valgrind ./a.out gives the following on gcc version 5.3. The program runs fine without a core dump and executed with valgrind. Otherwise, the program dumps core without valgrind. 

==29481== Memcheck, a memory error detector 
==29481== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==29481== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info 
==29481== Command: ./a.out 
==29481== 
one = ONE 
two = TWO 
three = THREE 
four = FOUR 
five = FIVE 
six = SIX 
==29481== Invalid write of size 8 
==29481== at 0x400645: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f60c0 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x4005F0: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
==29481== Invalid write of size 8 
==29481== at 0x40066E: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f60c0 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x4005F0: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
==29481== Invalid write of size 8 
==29481== at 0x400698: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f6130 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x400609: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
==29481== Invalid read of size 8 
==29481== at 0x4006B0: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f6130 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x400609: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
==29481== Invalid read of size 8 
==29481== at 0x4006C7: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f60c0 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x4005F0: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
seven = SEVEN 
eight = EIGHT 
nine = NINE 
ten = TEN 
==29481== Invalid free()/delete/delete[]/realloc() 
==29481== at 0x4C29E00: free (vg_replace_malloc.c:530) 
==29481== by 0x400722: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x400810 is in a r-x mapped file /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out segment 
==29481== 
==29481== Invalid read of size 8 
==29481== at 0x400718: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== Address 0x51f60c0 is 0 bytes after a block of size 48 alloc'd 
==29481== at 0x4C2AA98: calloc (vg_replace_malloc.c:711) 
==29481== by 0x4005F0: main (in /XtraStorage/netha/jayami/progs/cprog/gtk_examples/a.out) 
==29481== 
==29481== 
==29481== HEAP SUMMARY: 
==29481==  in use at exit: 4,040 bytes in 10 blocks 
==29481== total heap usage: 13 allocs, 13 frees, 4,152 bytes allocated 
==29481== 
==29481== LEAK SUMMARY: 
==29481== definitely lost: 4,040 bytes in 10 blocks 
==29481== indirectly lost: 0 bytes in 0 blocks 
==29481==  possibly lost: 0 bytes in 0 blocks 
==29481== still reachable: 0 bytes in 0 blocks 
==29481==   suppressed: 0 bytes in 0 blocks 
==29481== Rerun with --leak-check=full to see details of leaked memory 
==29481== 
==29481== For counts of detected and suppressed errors, rerun with: -v 
==29481== ERROR SUMMARY: 34 errors from 7 contexts (suppressed: 0 from 0) 

回答

1

val1 a nd val2是指針,但您使用sizeof(int)來分配它們。與*val1*val2相同。

0

感謝pointint出來。我一直在用sizeof(int)分配內存,並且沒有任何程序崩潰,直到這個新的gcc版本。我已經更新了代碼並將其粘貼到了gcc版本5.3的valgrind結果中。我將sizeof(int)替換爲sizeof(void *)

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


char *somval[12][2] = 
{ 
    {"one", "ONE"}, 
    {"two", "TWO"}, 
    {"three", "THREE"}, 
    {"four", "FOUR"}, 
    {"five", "FIVE"}, 
    {"six", "SIX"}, 
    {"seven", "SEVEN"}, 
    {"eight", "EIGHT"}, 
    {"nine", "NINE"}, 
    {"ten", "TEN"}, 
    {NULL, NULL} 
}; 

typedef struct _getval 
{ 
    char **val1; 
    char **val2; 
}GETVAL; 

int main() 
{ 
int i; 

GETVAL *getval; 
getval = calloc(1, sizeof(GETVAL)); 
getval->val1 = calloc(12, sizeof(void *)); 
getval->val2 = calloc(12, sizeof(void *)); 

for(i = 0; somval[i][1] != NULL; i++) 
    { 
    getval->val1[i] = somval[i][0]; 
    getval->val2[i] = somval[i][1]; 

    printf("%s = %s\n", getval->val1[i], getval->val2[i]); 
    } 

free(getval->val1); 
free(getval->val2); 
free(getval); 

return 0; 
} 

------------------------------------------- 
Compile with cc or gcc and ran with valgrind gave the below result. Without valgrind the program did not dump core. 

==31006== Memcheck, a memory error detector 
==31006== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 
==31006== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info 
==31006== Command: ./a.out 
==31006== 
one = ONE 
two = TWO 
three = THREE 
four = FOUR 
five = FIVE 
six = SIX 
seven = SEVEN 
eight = EIGHT 
nine = NINE 
ten = TEN 
==31006== 
==31006== HEAP SUMMARY: 
==31006==  in use at exit: 0 bytes in 0 blocks 
==31006== total heap usage: 3 allocs, 3 frees, 208 bytes allocated 
==31006== 
==31006== All heap blocks were freed -- no leaks are possible 
==31006== 
==31006== For counts of detected and suppressed errors, rerun with: -v 
==31006== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)