valgrind --tool=memcheck --leak-check=full ./test2 ==9674== Memcheck, a memory error detector ==9674== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==9674== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==9674== Command: ./test2 ==9674== ==9674== ==9674== HEAP SUMMARY: ==9674== in use at exit: 4 bytes in 1 blocks ==9674== total heap usage: 1 allocs, 0 frees, 4 bytes allocated #分配1次 释放0次 共分配4字节 ==9674== ==9674== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1 #发生1次泄露 ==9674== at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==9674== by 0x10915E: main (main.cpp:4) ==9674== ==9674== LEAK SUMMARY: ==9674== definitely lost: 4 bytes in 1 blocks ==9674== indirectly lost: 0 bytes in 0 blocks ==9674== possibly lost: 0 bytes in 0 blocks ==9674== still reachable: 0 bytes in 0 blocks ==9674== suppressed: 0 bytes in 0 blocks ==9674== ==9674== For lists of detected and suppressed errors, rerun with: -s ==9674== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
valgrind --tool=memcheck --leak-check=full ./test2 ==10459== Memcheck, a memory error detector ==10459== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==10459== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==10459== Command: ./test2 ==10459== ==10459== Invalid read of size 4 #越界读4字节 ==10459== at 0x1092AD: main (main.cpp:6) ==10459== Address 0x4e290a8 is 0 bytes after a block of size 40 alloc'd ==10459== at 0x4846FA3: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==10459== by 0x1099F3: std::__new_allocator<int>::allocate(unsigned long, void const*) (new_allocator.h:151) ==10459== by 0x1098E0: allocate (alloc_traits.h:482) ==10459== by 0x1098E0: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (stl_vector.h:381) ==10459== by 0x109758: std::_Vector_base<int, std::allocator<int> >::_M_create_storage(unsigned long) (stl_vector.h:398) ==10459== by 0x109594: std::_Vector_base<int, std::allocator<int> >::_Vector_base(unsigned long, std::allocator<int> const&) (stl_vector.h:335) ==10459== by 0x1093C0: std::vector<int, std::allocator<int> >::vector(unsigned long, int const&, std::allocator<int> const&) (stl_vector.h:571) ==10459== by 0x10928E: main (main.cpp:5) ==10459== 0 ==10459== ==10459== HEAP SUMMARY: ==10459== in use at exit: 0 bytes in 0 blocks ==10459== total heap usage: 3 allocs, 3 frees, 74,792 bytes allocated ==10459== ==10459== All heap blocks were freed -- no leaks are possible ==10459== ==10459== For lists of detected and suppressed errors, rerun with: -s ==10459== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
内存覆盖:
1 2 3 4 5 6 7 8 9 10 11 12
#include<string.h> #include<stdio.h> #include<stdlib.h> intmain(){ char x[50]; int i; for (i = 0; i < 50; i++) x[i] = i + 1; strncpy(x + 20, x, 20); strncpy(x + 20, x, 21); return0; }
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
valgrind --tool=memcheck --leak-check=full ./test2 ==11249== Memcheck, a memory error detector ==11249== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==11249== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==11249== Command: ./test2 ==11249== ==11249== Source and destination overlap in strncpy(0x1ffefffbd9, 0x1ffefffbc5, 21) #源地址和目标地址设置出现重叠 ==11249== at 0x484F5A0: strncpy (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==11249== by 0x1091DF: main (main.cpp:10) ==11249== ==11249== ==11249== HEAP SUMMARY: ==11249== in use at exit: 0 bytes in 0 blocks ==11249== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==11249== ==11249== All heap blocks were freed -- no leaks are possible ==11249== ==11249== For lists of detected and suppressed errors, rerun with: -s ==11249== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
内存未初始化:
1 2 3 4 5 6 7
#include<iostream> intmain(){ int x; if (x == 0) std::cout << "X is zero" << std::endl; return0; }
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
valgrind --tool=memcheck --leak-check=full ./test2 ==12034== Memcheck, a memory error detector ==12034== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==12034== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==12034== Command: ./test2 ==12034== ==12034== Conditional jump or move depends on uninitialised value(s) #访问未初始化内存 ==12034== at 0x109179: main (main.cpp:4) ==12034== ==12034== ==12034== HEAP SUMMARY: ==12034== in use at exit: 0 bytes in 0 blocks ==12034== total heap usage: 1 allocs, 1 frees, 73,728 bytes allocated ==12034== ==12034== All heap blocks were freed -- no leaks are possible ==12034== ==12034== Use --track-origins=yes to see where uninitialised values come from ==12034== For lists of detected and suppressed errors, rerun with: -s ==12034== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
valgrind --tool=memcheck --leak-check=full ./test2 ==12820== Memcheck, a memory error detector ==12820== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==12820== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==12820== Command: ./test2 ==12820== address [0x0x4e29080] ==12820== Mismatched free() / delete / delete [] ==12820== at 0x484A164: operator delete(void*) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==12820== by 0x10920C: main (main.cpp:9) ==12820== Address 0x4e29080 is 0 bytes inside a block of size 4 alloc'd ==12820== at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==12820== by 0x1091C6: main (main.cpp:5) ==12820== ==12820== ==12820== HEAP SUMMARY: ==12820== in use at exit: 0 bytes in 0 blocks ==12820== total heap usage: 3 allocs, 3 frees, 74,756 bytes allocated ==12820== ==12820== All heap blocks were freed -- no leaks are possible ==12820== ==12820== For lists of detected and suppressed errors, rerun with: -s ==12820== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)