// File: leaky.c // // This is the checker.c from ``Linux Programming'' by Mathew and Stones // // The code mallocs a buffer and then reads and writes beyond the end // of the buffer memory. The buffer is lost and not free. // valgrind can catch all of these errors and pinpoint the line // in the source code where they occur. // // How do I find a line number in my code? // // You can use a emacs short cut to goto line e.g., 20 in your code // Hold down the ESC key then hit the X key followed by the G key then 20 // then CR [carriage return]. You will be brought to line 20. This is // handy when gcc or valgrind or any tool for that matter tells you // there is a problem at a particular line. // #include #include int main() { char *ptr = (char *) malloc(1024); char ch; /* Uninitialized read */ ch = ptr[1024]; /* Write beyond the block */ ptr[1024] = 0; /* Orphan the block */ ptr = 0; exit(0); }