The following C program allocates 1024 bytes of memory and then reads the data from an area other than the allocated memory, writes the data after the end of the allocated memory, and finally makes the memory area inaccessible.
#include <stdio.h> #include <stdlib.h>int main () { char *ptr = (char *) malloc (1024x768); char ch; Uninitialized read ch = ptr[1024]; Write beyond the block ptr[1024] = 0; Orphan The block ptr = 0; return 0;}
Here are the results of running the Valgrind tool test: 3 memory management errors are all detected.
[[email protected] document]$ valgrind--leak-check=yes-v./checker==19044== Memcheck, a memory error detector== 19044== Copyright (C) 2002-2013, and GNU GPL ' d, by Julian Seward et al.==19044== Using Valgrind-3.10.1 and Libvex; Rerun with-h for copyright info==19044== Command:./checker==19044==--19044--Valgrind options:--19044----Leak-check =yes--19044---v--19044--Contents of/proc/version:--19044--Linux version 2.6.32-504.3.3.el6.x86_64 ([EMAIL&NBSP;PR Otected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (gcc)) #1 SMP Wed Dec 01:55:02 UTC 2014--19044--Arch and HWC Aps:amd64, Littleendian, amd64-cx16-rdtscp-sse3-avx--19044--Page sizes:currently 4096, Max supported 4096--19044--Val Grind Library Directory:/usr/local/lib/valgrind--19044--Reading syms from/home/zhang/document/checker--19044-- Reading syms from/usr/local/lib/valgrind/memcheck-amd64-linux--19044--Object doesn ' t has a dynamic symbol table--190 44--Reading syms from/lib64/ld-2.12.so--19044--scheduler:using Generic Scheduler lock implementation.--19044--Reading suppressions file:/usr/local/lib/ valgrind/default.supp==19044== Embedded Gdbserver:reading from/tmp/ vgdb-pipe-from-vgdb-to-19044-by-zhang-on-localhost.localdomain==19044== Embedded Gdbserver:writing to/tmp/ vgdb-pipe-to-vgdb-from-19044-by-zhang-on-localhost.localdomain==19044== Embedded gdbserver:shared mem/tmp/ vgdb-pipe-shared-mem-vgdb-19044-by-zhang-on-localhost.localdomain==19044== ==19044== to CONTROL this PROCESS USING Vgdb (which you probably==19044== don ' t want to does, unless you know exactly what you ' re doing,==19044== or is doing some Strange experiment): ==19044==/usr/local/lib/valgrind/. /.. /bin/vgdb--pid=19044 ... command...==19044== ==19044== to DEBUG this PROCESS USING gdb:start GDB like this==19044==/pa Th/to/gdb./checker==19044== and then give GDB the following command==19044== target remote | /usr/local/lib/valgrind/. /.. /bin/vgdb--pid=19044==19044==--pid is optional if oNly one valgrind process is running==19044==--19044--redir:0x3283e17610 (Ld-linux-x86-64.so.2:strlen) redirected to 0x3 8051201 (Vgplain_amd64_linux_redir_for_strlen)--19044--Reading syms From/usr/local/lib/valgrind/vgpreload_ core-amd64-linux.so--19044--Reading syms from/usr/local/lib/valgrind/vgpreload_memcheck-amd64-linux.so==19044== Warning:new redirection conflicts with existing--ignoring it--19044--old:0x3283e17610 (strlen) R-&G T (0000.0) 0x38051201 vgplain_amd64_linux_redir_for_strlen--19044--new:0x3283e17610 (strlen) R-> (200 7.0) 0x04a08960 strlen--19044--redir:0x3283e17480 (Ld-linux-x86-64.so.2:index) redirected to 0x4a08540 (index)--19044 --redir:0x3283e17500 (LD-LINUX-X86-64.SO.2:STRCMP) redirected to 0x4a09320 (strcmp)--19044--redir:0x3283e183f0 ( ld-linux-x86-64.so.2:mempcpy) redirected to 0x4a0bd80 (mempcpy)--19044--Reading syms from/lib64/ libc-2.12.so--19044--redir:0x3284284cd0 (LIBC.SO.6:STRCASECMP) redirectedTo 0X480155C (_vgnu_ifunc_wrapper)--19044--redir:0x3284286f90 (LIBC.SO.6:STRNCASECMP) redirected to 0X480155C (_vgnU_ Ifunc_wrapper)--19044--redir:0x3284282c40 (LIBC.SO.6:__GI_STRRCHR) redirected to 0x4a082d0 (__GI_STRRCHR)--19044-- redir:0x328427a640 (Libc.so.6:malloc) redirected to 0x4a07183 (malloc) ==19044== Invalid read of size 1==19044== at 0x4 004e4:main (in/home/zhang/document/checker) ==19044== Address 0x4c2b440 is 0 bytes after a block of size 1,024 alloc ' d== 19044== at 0x4a0720a:malloc (vg_replace_malloc.c:296) ==19044== by 0x4004d5:main (in/home/zhang/document/checker) = =19044== ==19044== Invalid Write of size 1==19044== at 0x4004f4:main (in/home/zhang/document/checker) ==19044== Addre SS 0x4c2b440 is 0 bytes after a block of size 1,024 alloc ' d==19044== at 0x4a0720a:malloc (vg_replace_malloc.c:296) ==19 044== by 0x4004d5:main (in/home/zhang/document/checker) ==19044==--19044--redir:0x328427b520 (libc.so.6:free) REDIR ected to 0x4a06b5d (free) ==19044= = ==19044== heap summary:==19044== in use @ exit:1,024 bytes in 1 blocks==19044== total HEAP usage:1 Allocs, 0 F Rees, 1,024 bytes allocated==19044== ==19044== searching for pointers to 1 not-freed blocks==19044== Checked 64,184 bytes= =19044== ==19044== 1,024 bytes in 1 blocks is definitely lost in loss record 1 of 1==19044== at 0x4a0720a:malloc (vg_ replace_malloc.c:296) ==19044== by 0x4004d5:main (in/home/zhang/document/checker) ==19044== ==19044== LEAK SUMMARY:==1 9044== definitely lost:1,024 bytes in 1 blocks==19044== indirectly lost:0 bytes in 0 blocks==19044== possibly lost:0 bytes in 0 blocks==19044== still reachable:0 bytes in 0 blocks==19044== suppressed:0 bytes in 0 bloc ks==19044== ==19044== ERROR Summary:3 errors from 3 contexts (Suppressed:4 to 4) ==19044== ==19044== 1 errors in Contex T 1 of 3:==19044== Invalid write of size 1==19044== at 0x4004f4:main (in/home/zhang/document/checker) ==19044== Addre SS 0x4c2b440 is 0 bytes after a block of size 1,024 alloc ' d==19044== at 0x4a0720a:malloc (vg_replace_malloc.c:296) ==19044== by 0x4004d5:mai N (in/home/zhang/document/checker) ==19044== ==19044== ==19044== 1 errors in context 2 of 3:==19044== Invalid read of size 1==19044== at 0x4004e4:main (in/home/zhang/document/checker) ==19044== Address 0x4c2b440 are 0 bytes after a block of Size 1,024 alloc ' d==19044== at 0x4a0720a:malloc (vg_replace_malloc.c:296) ==19044== by 0x4004d5:main (In/home/zha ng/document/checker) ==19044==--19044----19044--used_suppression:4 u1004-arm-_dl_relocate_object/usr/local/lib/v algrind/default.supp:1401==19044== ==19044== ERROR Summary:3 errors from 3 contexts (Suppressed:4 from 4)
Where the Valgrind tool can find it on http://valgrind.org.
Memory Debug--valgrind tool log group access errors and memory leak detection