FruitNotes beta
Your All-in-One Online Notebook
FruitNotes Blogs | Home  
Valgrind Tutorial
Last updated at (Tue Mar 11 2008 11:15:42)
Posted by: Ankur Barua
0%




ValgrindValgrind is a memory mismanagement detector. It shows you memory leaks, deallocation errors, etc. Actually, Valgrind is a wrapper around a collection of tools that do many other things (e.g., cache profiling); however, here we focus on the default tool, memcheck. Memcheck can detect:
  • Use of uninitialised memory
  • Reading/writing memory after it has been free'd
  • Reading/writing off the end of malloc'd blocks
  • Reading/writing inappropriate areas on the stack
  • Memory leaks -- where pointers to malloc'd blocks are lost forever
  • Mismatched use of malloc/new/new [] vs free/delete/delete []
  • Overlapping src and dst pointers in memcpy() and related functions
  • Some misuses of the POSIX pthreads API
To use this on our example program, test.c, try
gcc -o test -g test.c
This creates an executable named test. To check for memory leaks during the execution of test, try
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test
This outputs a report to the terminal like ==9704== Memcheck, a memory error detector for x86-linux.
==9704== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==9704== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==9704== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==9704== For more details, rerun with: -v
==9704==
==9704==
==9704== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==9704== malloc/free: in use at exit: 35 bytes in 2 blocks.
==9704== malloc/free: 3 allocs, 1 frees, 47 bytes allocated.
==9704== For counts of detected errors, rerun with: -v
==9704== searching for pointers to 2 not-freed blocks.
==9704== checked 1420940 bytes. \
==9704==
==9704== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==9704== at 0x1B903D38: malloc (vg_replace_malloc.c:131)
==9704== by 0x80483BF: main (test.c:15)
==9704==
==9704==
==9704== 19 bytes in 1 blocks are definitely lost in loss record 2 of 2
==9704== at 0x1B903D38: malloc (vg_replace_malloc.c:131)
==9704== by 0x8048391: main (test.c:8)
==9704==
==9704== LEAK SUMMARY: ==9704== definitely lost: 35 bytes in 2 blocks.
==9704== possibly lost: 0 bytes in 0 blocks. ==9704== still reachable: 0 bytes in 0 blocks.
==9704== suppressed: 0 bytes in 0 blocks.
Let's look at the code to see what happened. Allocation #1 (19 byte leak) is lost because p is pointed elsewhere before the memory from Allocation #1 is free'd. To help us track it down, Valgrind gives us a stack trace showing where the bytes were allocated. In the 19 byte leak entry, the bytes were allocate in test.c, line 8. Allocation #2 (12 byte leak) doesn't show up in the list because it is free'd. Allocation #3 shows up in the list even though there is still a reference to it (p) at program termination. This is still a memory leak! Again, Valgrind tells us where to look for the allocation (test.c line 15).

Valgrind can detect many kinds of errors. Here's an explanation of the various error messages.

 ====================

test.c

#include <stdio.h>
int main()
{ 
    char *p; 
   // Allocation #1 of 19 bytes  
   p = (char *) malloc(19); 
   // Allocation #2 of 12 bytes  
   p = (char *) malloc(12); 
   free(p);  
  // Allocation #3 of 16 bytes  
  p = (char *) malloc(16); 
   return 0;
}

Rate this blog

   Report Abuse


Comments


From Adi Adi at Fri Jan 21 2005 22:39:30 GMT 0500 (Pakistan Standard Time)

The discovery and utilization of fire, a simple cheap web hosting energy source with many profound uses, was a turning point in the technological evolution of humankind.[21] The exact date of its discovery is not known; wireless internet evidence of burnt animal bones at the Cradle of Humankind suggests that the domestication of fire occurred before 1,000,000 BC;[22] scholarly consensus indicates that Homo erectus had controlled fire by between 500,000 BC and 400,000 BC.dedicated server Fire, fueled with wood and charcoal, allowed early humans to cook their food to increase its digestibility, improving its nutrient value and broadening the number vps hosting of foods that could be eaten.[25]

-----------

Leave your comment(s) below:
To start Your own Blog




Other Blogs
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
» 
2007 FruitNotes.com - All Rights Reserved.