Saturday 12 November 2011

How can I detect memory leaks? in C programming

How can I detect memory leaks?

A memory leak occurs when dynamically allocated memory—that is, memory that has been allocated using
a form of malloc() or calloc()—is not deleted when it is no longer needed. Not freeing memory is not an error in itself; the compiler will not complain, and your program will not crash immediately when memory is not freed. The effect is that as more and more unused memory fails to be freed, the free space available to the program for new data will shrink. Eventually, when the program tries to allocate storage, it will find that none is available. This situation can cause the program to behave oddly, especially if the programmer has not accounted for the possibility of memory allocation failing.

Memory leaks are one of the most difficult errors to detect, as well as some of the most dangerous. This is
because the programming error that causes the problem can be made very early on in the development of the program, but the error will not become apparent until later, when the program mysteriously runs out of memory when it is run “for real.” Looking at the line that contains the failed allocation also will not help. The line of the program that allocated memory and failed to free it might be somewhere else entirely.

Unfortunately, the C language has no built-in way to detect or fix memory leaks. Aside from commercial
packages that repair or detect memory leaks, detecting and repairing memory leaks requires a great deal of
patience and care on the part of the programmer. It is far better to keep the possibility of memory leaks in mind while developing the program and to exercise great caution concerning them.

The simplest, and perhaps most common, cause of memory leakage is forgetting to free memory that has been
allocated for use as temporary scratch space, as in the following code fragment:

#include <stdio.h>
#include <stdlib.h>
/*
* Say hello to the user, and put the user’s name in UPPERCASE.
*/
void SayHi( char *name )
{
char *UpName;
int a;
UpName = malloc( strlen( name ) + 1 );
/* Allocate space for the name */
for( a = 0 ; a < strlen( name ) ; ++ a )
UpName[ a ] = toupper( name[ a ] );
UpName[ a ] = ‘\0’;
printf( “Hello, %s!\n” , UpName );
}
int main()
{
SayHi( “Dave” );
return( 0 );
}
Of course, the problem here is easy to see—the program allocates temporary space for the storage of the
uppercase version of the name but never frees it. There is a simple way to ensure that this problem will never
happen. Whenever temporary space is allocated, immediately type the corresponding free statement, and insert the code that uses the temporary space in between them. This method ensures that every allocated block
of memory will be cleaned up when it is no longer needed, as long as the program does not somehow leave
the space in between allocation and freeing by break, continue, or the evil goto.

If this was all there was to fixing memory leaks, it would be no problem—this is a rather trivial matter to fix.
In the real world of programming, however, blocks of memory are allocated and often are needed for an
undetermined period; memory leakage might result if the code that handles or deletes memory blocks is in
some way flawed. For example, in the process of deleting a linked list, a last node might be missed, or a pointer

that is pointing to a block of memory might be overwritten. These kinds of problems can be fixed only by
careful and meticulous programming or, as has already been mentioned, by packages that track memory, or
by language extensions.

Cross Reference:

XI.1: My program hangs when I run it. What should I do?

No comments:

Post a Comment