The following is a reformatted excerpt of a very helpful article from 2006 by Rakesh Shrestha about writing crash-proof programs.
Causes of Crashes
|
Accessing or deleting memory you do not own
|
Uncaught exceptions
|
- Dereferencing a NULL pointer
*(NULL)
NULL->member
NULL[1]
NULL->function()
strcpy( NULL, "hello" )
*(NULL)(params);
this == NULL during an implicit (*this).
- Dereferencing an uninitialized pointer
blah* pPointer; *pPointer
All same cases as (1)
- Dereferencing a deleted pointer
delete pPointer; *pPointer
All same cases as (1)
- Deleting an uninitialized pointer
blah* pPointer; delete pPointer;
- Deleting a pointer twice
delete pPointer; delete pPointer;
- Deleting non-dynamic memory
int x; int* p = &x; delete p;
- Writing beyond the bounds of an array
int x[10]; x[-1] = 1;
int x[10]; x[10] = 1;
(a) and (b) but hidden in loops
|
- Divide by zero
int x = 0; 2/x
double x = 0.0; 2.0/x
int x = 0; 2%x
You will also see overflow and underflow occasionally
- Stack overflow
- Infinitely recursive function
void InfiniteRecurse( int x ) {
if ( false ) {
// terminating condition which is never met
return;
}
else {
// recurse condition which is always met
InifiniteRecurse(x+1);
}
}
- Infinitely recursive set of functions
Same as (a) but a set of functions are mutually recursive, so the call stack looks like a -> b -> c -> a -> b -> c -> a -> b -> c -> a -> b -> c -> ...
- Valid recursive function but each call using too much stack space
void BigRecurse( unsigned int x ) {
int aBigArray[1000];
if( x >= 1000 ) {
return;
}
else {
aBigArray[x] = x;
BigRecurse(x+1);
}
}
- Out of memory; this may show up as an exception on some systems, others will just return NULL from the new or malloc (Visual C++’s C library returns NULL and does not throw an exception).
int* p = new int;
- User or library code generated exceptions that failed to get wrapped in a try/catch. Third party code may throw exceptions under some circumstances. Your code might intentionally throw exceptions. If these miss getting caught then the exceptions will make it all the way to the top of the thread.
ret = ThisFunctionThrowsAnException();
|