/* <Br/> # filename: test1.c <br/> This example demonstrates that when space is allocated to a global pointer, no memory leakage occurs if free is called, because the system will automatically recycle it. Pass the </P> <p> valgrind test. </P> <p> */</P> <p> # include <stdlib. h> <br/> # include <signal. h> <br/> # include <stdio. h> <br/> # include <stdbool. h> // use bool <br/> static int bisexit = 0; <br/> int * x = NULL; // define global point <br/> void F (void) <br/>{< br/> X = malloc (10 * sizeof (INT); <br/>}< br/> void sighandler (INT signo) <br/>{< br/> bisexit = true; <br/>}< br/> int main (INT argc, char ** argv) <br/>{< br/> bisexit = false; </P> <p> F (); <br/> If (signal (SIGINT, sighandler) = sig_err) <br/>{< br/> printf ("signal SIGINT error "); <br/> return 0; <br/>}< br/> If (signal (sigterm, sighandler) = sig_err) <br/>{< br/> printf ("signal sigterm error"); <br/> return 0; <br/>}< br/> while (! Bisexit) <br/>{< br/> // sleep (2); <br/>}< br/>/* If (X) <br/>{< br/> free (x); <br/> X = NULL; <br/>}< br/> */<br/> printf ("succeed exit! "); <Br/> return 0; </P> <p>}
$ Gcc-wall test1.c-g-o test1
$ Valgrind -- tool = memcheck -- leak-check = full./test1
==3391 = memcheck, a memory error detector.
= 3391 = copyright (c) 2002-2008, and gnu gpl 'd, by Julian Seward et al.
= 3391 = Using libvex rev 1878, a library for Dynamic Binary Translation.
= 3391 = copyright (c) 2004-2008, and gnu gpl 'd, by openworks LLP.
= 3391 = Using valgrind-3.4.0, a dynamic binary instrumentation framework.
= 3391 = copyright (c) 2000-2008, and gnu gpl 'd, by Julian Seward et al.
= 3391 = For more details, Rerun with:-V
= 3391 =
= 3391 =
= 3391 = Error Summary: 0 errors from 0 contexts (suppressed: 3 from 1)
= 3391 = malloc/free: in use at Exit: 40 bytes in 1 blocks.
= 3391 = malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
= 3391 = for counts of detected errors, Rerun with:-V
= 3391 = searching for pointers to 1 not-freed blocks.
= 3391 = checked 57,196 bytes.
= 3391 =
= 3391 = leak summary:
= 3391 = definitely lost: 0 bytes in 0 blocks.
==3391 = possibly lost: 0 bytes in 0 blocks.
= 3391 = still reachable: 40 bytes in 1 blocks.
= 3391 = suppressed: 0 bytes in 0 blocks.
= 3391 = reachable blocks (those to which a pointer was found) are not shown.
= 3391 = to see them, Rerun with: -- leak-check = full -- show-reachable = Yes
We can see that there is no memory leakage.
/* <Br/> # filename: test2.c <br/> This example demonstrates that when a local pointer is allocated space, the system does not automatically recycle it, and memory leakage may occur, test by valgrind. </P> <p> */</P> <p> # include <stdlib. h> <br/> # include <signal. h> <br/> # include <stdio. h> <br/> # include <stdbool. h> // use bool <br/> static int bisexit = 0; <br/> // int * x = NULL; <br/> void F (void) <br/>{< br/> int * x = malloc (10 * sizeof (INT); <br/> X [0] = 0; <br/>}</P> <p> void sighandler (INT signo) <br/>{< br/> bisexit = true; <br/>}< br/> int main (INT argc, char ** argv) <br/>{< br/> bisexit = false; </P> <P> F (); <br/> If (signal (SIGINT, sighandler) = sig_err) <br/>{< br/> printf ("signal SIGINT error"); <br/> return 0; <br/>}< br/> If (signal (sigterm, sighandler) = sig_err) <br/>{< br/> printf ("signal sigterm error"); <br/> return 0; <br/>}< br/> while (! Bisexit) <br/>{< br/> // sleep (2); <br/>}< br/>/* If (X) <br/>{< br/> free (x); <br/> X = NULL; <br/>}< br/> */<br/> printf ("succeed exit! "); <Br/> return 0; </P> <p>}
$ Gcc-wall test2.c-g-o Test2
$ Valgrind -- tool = memcheck -- leak-check = full./Test2
Gcc-wall 1.C-g-o Test2
~ /20090212 $ valgrind -- tool = memcheck -- leak-check = Full Test2
==3479 = memcheck, a memory error detector.
= 3479 = copyright (c) 2002-2008, and gnu gpl 'd, by Julian Seward et al.
= 3479 = Using libvex rev 1878, a library for Dynamic Binary Translation.
= 3479 = copyright (c) 2004-2008, and gnu gpl 'd, by openworks LLP.
= 3479 = Using valgrind-3.4.0, a dynamic binary instrumentation framework.
= 3479 = copyright (c) 2000-2008, and gnu gpl 'd, by Julian Seward et al.
= 3479 = For more details, Rerun with:-V
= 3479 =
Succeed exit! = 3479 =
= 3479 = Error Summary: 0 errors from 0 contexts (suppressed: 3 from 1)
= 3479 = malloc/free: in use at Exit: 40 bytes in 1 blocks.
= 3479 = malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
= 3479 = for counts of detected errors, Rerun with:-V
= 3479 = searching for pointers to 1 not-freed blocks.
= 3479 = checked 57,196 bytes.
= 3479 =
==3479 = 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
= 3479 = at 0x4021b38: malloc (vg_replace_malloc.c: 207)
= 3479 = by 0x8048435: F (1.c: 9)
= 3479 = by 0x8048472: Main (1.c: 21)
= 3479 =
= 3479 = leak summary:
= 3479 = definitely lost: 40 bytes in 1 blocks.
==3479 = possibly lost: 0 bytes in 0 blocks.
= 3479 = still reachable: 0 bytes in 0 blocks.
= 3479 = suppressed: 0 bytes in 0 blocks.
We can see memory leakage.
The above example shows that it is best to release the memory allocated by the malloc system function to avoid Memory leakage.