Previous      Up      Previous     Course Home   e-mail

2.2 Pointers Can Be Dangerous

Because pointers provide access a memory location and because data and executable code exist in memory together, misuses of pointers can lead to both  bizarre effects and very subtle errors.

Potential Problems with Pointers   


Uninitialized pointers   


Memory Leaks   

A memory leak occurs when all pointers to a value allocated on the heap has been lost, e.g.,


int isqrt ( int i )
{
int * work = new int ;
* work = i ;
while (( * work ) * ( * work ) > i )
- - ( * work );
return * work ;
}

Over time, memory leaks can cause programs to slow down and, eventually, crash.

Worse, a leaky program may come to take up so much of a systems memory that it interferes with the operation of other programs on the same system.

Dangling Pointers   

Dangling pointers refer to a pointer which was pointing at an object that has been deleted.


int * p = new int ;
int * q = p ;

delete p ;

 Previous      Up      Previous     Course Home   e-mail