➜ code valgrind --leak-check=full ./test ==9289== Memcheck, a memory error detector ==9289== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==9289== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==9289== Command: ./test ==9289== ==9289== Invalid write of size 4 ==9289== at 0x10916B: k (unrelease.c:5) ==9289== by 0x109180: main (unrelease.c:10) ==9289== Address 0x4a86064 is 4 bytes after a block of size 32 alloc'd ==9289== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==9289== by 0x10915E: k (unrelease.c:4) ==9289== by 0x109180: main (unrelease.c:10) ==9289== ==9289== ==9289== HEAP SUMMARY: ==9289== in use at exit: 32 bytes in 1 blocks ==9289== total heap usage: 1 allocs, 0 frees, 32 bytes allocated ==9289== ==9289== 32 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==9289== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==9289== by 0x10915E: k (unrelease.c:4) ==9289== by 0x109180: main (unrelease.c:10) ==9289== ==9289== LEAK SUMMARY: ==9289== definitely lost: 32 bytes in 1 blocks ==9289== indirectly lost: 0 bytes in 0 blocks ==9289== possibly lost: 0 bytes in 0 blocks ==9289== still reachable: 0 bytes in 0 blocks ==9289== suppressed: 0 bytes in 0 blocks ==9289== ==9289== For lists of detected and suppressed errors, rerun with: -s ==9289== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
➜ code valgrind --leak-check=full ./test ==10526== Memcheck, a memory error detector ==10526== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==10526== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==10526== Command: ./test ==10526== ==10526== ==10526== HEAP SUMMARY: ==10526== in use at exit: 0 bytes in 0 blocks ==10526== total heap usage: 1 allocs, 1 frees, 32 bytes allocated ==10526== ==10526== All heap blocks were freed -- no leaks are possible ==10526== ==10526== For lists of detected and suppressed errors, rerun with: -s ==10526== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
➜ code valgrind --leak-check=full ./test ==12246== Memcheck, a memory error detector ==12246== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==12246== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==12246== Command: ./test ==12246==
[a] ==12246== Invalid read of size 1 ==12246== at 0x1091DE: main (release.c:14) ==12246== Address 0x4a86040 is 0 bytes inside a block of size 1 free'd ==12246== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==12246== by 0x1091D9: main (release.c:13) ==12246== Block was alloc'd at ==12246== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==12246== by 0x10919E: main (release.c:6) ==12246== ==12246== ==12246== HEAP SUMMARY: ==12246== in use at exit: 0 bytes in 0 blocks ==12246== total heap usage: 2 allocs, 2 frees, 1,025 bytes allocated ==12246== ==12246== All heap blocks were freed -- no leaks are possible ==12246== ==12246== For lists of detected and suppressed errors, rerun with: -s ==12246== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
➜ code valgrind --leak-check=full ./test ==14592== Memcheck, a memory error detector ==14592== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==14592== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==14592== Command: ./test ==14592== ==14592== Invalid read of size 1 ==14592== at 0x1091AE: main (access.c:8) ==14592== Address 0x4a86041 is 0 bytes after a block of size 1 alloc'd ==14592== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==14592== by 0x10919E: main (access.c:6) ==14592==
[] ==14592== ==14592== HEAP SUMMARY: ==14592== in use at exit: 0 bytes in 0 blocks ==14592== total heap usage: 2 allocs, 2 frees, 1,025 bytes allocated ==14592== ==14592== All heap blocks were freed -- no leaks are possible ==14592== ==14592== For lists of detected and suppressed errors, rerun with: -s ==14592== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
内存泄漏
1 2 3 4 5 6 7 8 9 10 11
#include<stdio.h> #include<stdlib.h>
intmain(void) { int *p = malloc(1); *p = 'x'; char c = *p; printf("%c\n", c); // 申请后未释放 return0; }
➜ code valgrind --leak-check=full ./test ==15397== Memcheck, a memory error detector ==15397== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==15397== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==15397== Command: ./test ==15397== ==15397== Invalid write of size 4 ==15397== at 0x109187: main (leakage.c:7) ==15397== Address 0x4a86040 is 0 bytes inside a block of size 1 alloc'd ==15397== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==15397== by 0x10917E: main (leakage.c:6) ==15397== x ==15397== ==15397== HEAP SUMMARY: ==15397== in use at exit: 1 bytes in 1 blocks ==15397== total heap usage: 2 allocs, 1 frees, 1,025 bytes allocated ==15397== ==15397== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==15397== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==15397== by 0x10917E: main (leakage.c:6) ==15397== ==15397== LEAK SUMMARY: ==15397== definitely lost: 1 bytes in 1 blocks ==15397== indirectly lost: 0 bytes in 0 blocks ==15397== possibly lost: 0 bytes in 0 blocks ==15397== still reachable: 0 bytes in 0 blocks ==15397== suppressed: 0 bytes in 0 blocks ==15397== ==15397== For lists of detected and suppressed errors, rerun with: -s ==15397== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
➜ code valgrind --leak-check=full ./test ==17704== Memcheck, a memory error detector ==17704== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==17704== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==17704== Command: ./test ==17704== ==17704== Conditional jump or move depends on uninitialised value(s) ==17704== at 0x484ED19: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x48D1DB0: __vfprintf_internal (vfprintf-internal.c:1517) ==17704== by 0x48BB81E: printf (printf.c:33) ==17704== by 0x1091C4: main (releases.c:8) ==17704== ==17704== Invalid free() / delete / delete[] / realloc() ==17704== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x1091F2: main (releases.c:12) ==17704== Address 0x4a86040 is 0 bytes inside a block of size 100 free'd ==17704== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x1091E6: main (releases.c:11) ==17704== Block was alloc'd at ==17704== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x10919E: main (releases.c:6) ==17704== ==17704== Invalid free() / delete / delete[] / realloc() ==17704== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x1091FE: main (releases.c:13) ==17704== Address 0x4a86040 is 0 bytes inside a block of size 100 free'd ==17704== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x1091E6: main (releases.c:11) ==17704== Block was alloc'd at ==17704== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==17704== by 0x10919E: main (releases.c:6) ==17704== Memory Allocated at: /n==17704== ==17704== HEAP SUMMARY: ==17704== in use at exit: 0 bytes in 0 blocks ==17704== total heap usage: 2 allocs, 4 frees, 1,124 bytes allocated ==17704== ==17704== All heap blocks were freed -- no leaks are possible ==17704== ==17704== Use --track-origins=yes to see where uninitialised values come from ==17704== For lists of detected and suppressed errors, rerun with: -s ==17704== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
➜ code valgrind --leak-check=full ./test ==18498== Memcheck, a memory error detector ==18498== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==18498== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==18498== Command: ./test ==18498== ==18498== Invalid write of size 1 ==18498== at 0x1091C9: main (new.c:13) ==18498== Address 0x4a86041 is 1 bytes inside a block of size 10 free'd ==18498== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18498== by 0x1091C0: main (new.c:12) ==18498== Block was alloc'd at ==18498== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18498== by 0x109185: main (new.c:6) ==18498== ==18498== Invalid free() / delete / delete[] / realloc() ==18498== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18498== by 0x1091D7: main (new.c:14) ==18498== Address 0x4a86040 is 0 bytes inside a block of size 10 free'd ==18498== at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18498== by 0x1091C0: main (new.c:12) ==18498== Block was alloc'd at ==18498== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==18498== by 0x109185: main (new.c:6) ==18498== ==18498== ==18498== HEAP SUMMARY: ==18498== in use at exit: 0 bytes in 0 blocks ==18498== total heap usage: 1 allocs, 2 frees, 10 bytes allocated ==18498== ==18498== All heap blocks were freed -- no leaks are possible ==18498== ==18498== For lists of detected and suppressed errors, rerun with: -s ==18498== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
➜ valgrind valgrind --leak-check=full ./test ==20940== Memcheck, a memory error detector ==20940== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==20940== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==20940== Command: ./test ==20940== ==20940== Source and destination overlap in strncpy(0x1ffefffae9, 0x1ffefffad5, 21) ==20940== at 0x484F084: strncpy (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==20940== by 0x1091DF: main (1.c:16) ==20940== ==20940== ==20940== HEAP SUMMARY: ==20940== in use at exit: 0 bytes in 0 blocks ==20940== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==20940== ==20940== All heap blocks were freed -- no leaks are possible ==20940== ==20940== For lists of detected and suppressed errors, rerun with: -s ==20940== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)