Tôi đang cố gắng tìm hiểu một vài điều (giống như sở thích) và cố gắng học cách sử dụng Valgrind. Tuy nhiên điều này dường như không có ý nghĩa với tôi. Có vẻ như Valgrind đang nói rằng các byte bị mất khi tôi phân bổ chúng với calloc trước khi tôi thậm chí sử dụng bất cứ điều gì! Ai đó có thể giải thích những gì đang xảy ra ở đây và tại sao chương trình thứ hai lại hoạt động? Tôi đã biên dịch các chương trình trong chế độ gỡ rối trong Eclipse và chạy Valgrind trên thực thi gỡ rối.Tại sao Valgrind hiển thị rò rỉ bộ nhớ trên một tuyên bố calloc
Dưới đây là các chương trình:
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 int main(void) {
6
7 char* origstr = calloc(37, sizeof(char*));
8 char* newsubstr = calloc(9, sizeof(char*));
9
10 origstr = "TheQuickBrownFoxJumpedOverTheLazyDog";
11
12 strncpy(newsubstr, origstr + 8, 8);
13 printf("SubString is: %s\n", newsubstr);
14
15 free(newsubstr);
16 free(origstr);
17 return 0;
18 }
Và đây là những gì Valgrind mang lại cho tôi:
$ valgrind --tool=memcheck --leak-check=full ./test
==25404== Memcheck, a memory error detector
==25404== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25404== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==25404== Command: ./test
==25404==
SubString is: BrownFox
==25404== Invalid free()/delete/delete[]/realloc()
==25404== at 0x4C29E90: free (vg_replace_malloc.c:473)
==25404== by 0x400665: main (test.c:16)
==25404== Address 0x4006f8 is not stack'd, malloc'd or (recently) free'd
==25404==
==25404==
==25404== HEAP SUMMARY:
==25404== in use at exit: 296 bytes in 1 blocks
==25404== total heap usage: 2 allocs, 2 frees, 368 bytes allocated
==25404==
==25404== 296 bytes in 1 blocks are definitely lost in loss record 1 of 1
==25404== at 0x4C2AD10: calloc (vg_replace_malloc.c:623)
==25404== by 0x4005FC: main (test.c:7)
==25404==
==25404== LEAK SUMMARY:
==25404== definitely lost: 296 bytes in 1 blocks
==25404== indirectly lost: 0 bytes in 0 blocks
==25404== possibly lost: 0 bytes in 0 blocks
==25404== still reachable: 0 bytes in 0 blocks
==25404== suppressed: 0 bytes in 0 blocks
==25404==
==25404== For counts of detected and suppressed errors, rerun with: -v
==25404== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Nếu tôi loại bỏ hai) báo cáo (miễn phí, đây là những gì Valgrind mang lại cho tôi:
$ valgrind --tool=memcheck --leak-check=full ./test
==25597== Memcheck, a memory error detector
==25597== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25597== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==25597== Command: ./test
==25597==
SubString is: BrownFox
==25597==
==25597== HEAP SUMMARY:
==25597== in use at exit: 368 bytes in 2 blocks
==25597== total heap usage: 2 allocs, 0 frees, 368 bytes allocated
==25597==
==25597== 72 bytes in 1 blocks are definitely lost in loss record 1 of 2
==25597== at 0x4C2AD10: calloc (vg_replace_malloc.c:623)
==25597== by 0x4005BF: main (test.c:8)
==25597==
==25597== 296 bytes in 1 blocks are definitely lost in loss record 2 of 2
==25597== at 0x4C2AD10: calloc (vg_replace_malloc.c:623)
==25597== by 0x4005AC: main (test.c:7)
==25597==
==25597== LEAK SUMMARY:
==25597== definitely lost: 368 bytes in 2 blocks
==25597== indirectly lost: 0 bytes in 0 blocks
==25597== possibly lost: 0 bytes in 0 blocks
==25597== still reachable: 0 bytes in 0 blocks
==25597== suppressed: 0 bytes in 0 blocks
==25597==
==25597== For counts of detected and suppressed errors, rerun with: -v
==25597== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Bây giờ, nếu tôi chạy chương trình này:
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 int main(void) {
6
7 char* origstr;
8 char* newsubstr = calloc(9, sizeof(char*));
9
10 origstr = "TheQuickBrownFoxJumpedOverTheLazyDog";
11
12 strncpy(newsubstr, origstr + 8, 8);
13 printf("SubString is: %s\n", newsubstr);
14
15 free(newsubstr);
16
17 return 0;
18 }
Nó cho thấy tất cả mọi thứ chỉ là tốt:
$ valgrind --tool=memcheck --leak-check=full ./test
==25862== Memcheck, a memory error detector
==25862== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==25862== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==25862== Command: ./test
==25862==
SubString is: BrownFox
==25862==
==25862== HEAP SUMMARY:
==25862== in use at exit: 0 bytes in 0 blocks
==25862== total heap usage: 1 allocs, 1 frees, 72 bytes allocated
==25862==
==25862== All heap blocks were freed -- no leaks are possible
==25862==
==25862== For counts of detected and suppressed errors, rerun with: -v
==25862== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Tại sao nó mà tôi có thể không calloc (phân bổ) origstr và sau đó cung cấp cho nó một cái gì đó? Điều gì sẽ xảy ra nếu tôi muốn phân bổ biến đó và trong quá trình của chương trình, hãy cho nó một phần của biến nào đó trong biến chuỗi khác hoặc sử dụng nó để nắm bắt kết quả của hàm khác trả về một chuỗi? Sau đó tôi sẽ phải xử lý nó như tôi đã làm newsubstr?
Điều này hơi khó hiểu với tôi vì vậy ai đó có thể giải thích cách hoạt động của nó để tôi có thể hiểu nó tốt hơn?
Bạn đang mất (rò rỉ) 'cái calloc() 'con trỏ ed với nhiệm vụ trên đường dây 10 – Kninnug
' origstr = "TheQuickBrownFoxJumpedOverTheLazyDog";' Điều này không _not_ thiết lập các nội dung của 'origstr'; nó đặt 'origstr' để trỏ đến phần chỉ đọc của bộ nhớ có chứa chuỗi chữ. Bộ nhớ mà bạn đã cấp trước đó vào 'origstr' bị mất và bạn không thể gọi' free' trên chuỗi ký tự. –