background image
<< Introduction | NULL dereferencing >>

Bugs with Pointers and Memory

<< Introduction | NULL dereferencing >>
· Minor: Defects that can cause small or negligible consequences for the system. Ex. Displaying
result in some different format.
· No Effect: These may not necessarily hamper the system performance, but they may give
slightly different interpretation and generally avoidable. Ex. simple typographic errors in
documentation.
Goal of this TR:
In this TR we compile a list of bugs commonly found in programs. The compilation is based on
various published articles on the topic and users experience in programming. The aim is to educate
the programmers about frequent mistakes, so that they can avoid them. In addition, we also describe
some programming practices - both do's and dont's, that can in general help to reduce the incidence
of bugs.
2
Common Bugs
This section lists common bugs which occur during coding which directly or indirectly manifest
themselves to a larger damage to the running program.
2.1
Bugs with Pointers and Memory
The following are various possible bugs in dealing with pointers and memory.
2.1.1
Memory leaks
(frequent, catastrophic) [6]
Memory leak is a common programming bug which occurs frequently in the languages which
don't have automatic garbage collection (like C, C++). A memory leak is a situation, where the
memory is allocated to the program which is not freed subsequently. This kind of situation can
cause ever increasing usage of memory and hence at some point of time, the program may have to
come to an exceptional halt because of the lack of free memory [22] [4] . An example of this error is:
c h a r * foo ( int s )
{
c h a r * o u t p u t ;
if ( s >0)
o u t p u t =( c h a r *) m a l l o c ( s i z e );
if ( s = = 1 )
r e t u r n N U L L ;
r e t u r n ( o u t p u t );
/* if s >0 and s ==1 then ,
* a l l o c a t e d m e m o r y for o u t p u t
* is l e a k e d */
}
2.1.2
Temporary values returned
(rare, catastrophic)
The variables declared in a function represents some data which is private to that function. If
the data is not dynamically allocated, it will be stored in the stack otherwise it will be stored in the
heap. If we return the statically allocated variables address, then we are giving access to programs
stack. If some data needs to be shared, then declare the variable as public and use that to access
the common data.
2