KNU: Check for the following conditions
Accessing or deleting memory you do not own
1. Dereferencing a NULL pointer
a. *(NULL)
b. NULL->member
c. NULL[1]
d. NULL->function()
e. strcpy( NULL, "hello" )
f. *(NULL)(params);
g. this == NULL during an implicit (*this).
2. Dereferencing an uninitialized pointer
a. blah* pPointer; *pPointer
b. All same cases as (1)
3. Dereferencing a deleted pointer
a. delete pPointer; *pPointer
b. All same cases as (1)
4. Deleting an uninitialized pointer
a. blah* pPointer; delete pPointer;
5. Deleting a pointer twice
a. delete pPointer; delete pPointer;
6. Deleting non-dynamic memory
a. int x; int* p = &x; delete p;
http://devcentral.iftech.com/articles/C++/nocrashes/ (1 of 4) [3/15/2002 6:17:17 PM]
DevCentral - Writing Crash-Proof C/C++ Code
7. Writing beyond the bounds of an array
a. int x[10]; x[-1] = 1;
b. int x[10]; x[10] = 1;
c. (a) and (b) but hidden in loops
Uncaught exceptions
1. Divide by zero
a. int x = 0; 2/x
b. double x = 0.0; 2.0/x
c. int x = 0; 2%x
d. You will also see overflow and underflow occasionally
2. Stack overflow
a. 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);
}
}
b. 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 -> ...
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);
}
}
3. 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).
a. int* p = new int;
4. 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.
a. ret = ThisFunctionThrowsAnException();
