Insure++ Reference - EXPR_NULL
This error is generated whenever an expression operates on the
NULL pointer.
The following code fragment declares a pointer, a ,
which is initialized to zero by virtue of being a global variable. It
then manipulates this pointer, generating the EXPR_NULL
error.
1: /*
2: * File: expnull.c
3: */
4: char *a;
5:
6: main()
7: {
8: char *b;
9:
10: b = &a[1];
11: return (0);
12: }
1. [expnull.c:10] **EXPR_NULL**
>> b = &a[1];
2. Expression uses null pointer: a[1]
3. Stack trace where the error occurred:
main() expnull.c, 10
- Source line at which the problem was detected.
- Description of the problem and the expression that is in error.
- Stack trace showing the function call sequence leading to the
error.
One potential cause of this error is shown in this example. The
pointer a is a global variable that will be
initialized to zero by the compiler. Since this variable is never
modified to point to anything else, it is still NULL
when first used.
One way the given code can be corrected is by adding an assignment
as follows
/*
* File: expnull.c (modified)
*/
char *a;
main()
{
char *b, c[10];
a = c;
b = &a[1];
return (0);
}
It could also be corrected by allocating a block of memory.
A second possibility is that the pointer was set to zero by the
program at some point before its subsequent use and not re-initialized.
This is common in programs which make heavy use of dynamically allocated
memory and which mark freed blocks by resetting their pointers to
NULL .
A final common problem is caused when one of the dynamic memory
allocation routines, malloc, calloc , or
realloc , fails and returns a NULL
pointer. This can happen either because your program passes bad arguments
or simply because it asks for too much memory. A simple way of finding
this problem with Insure++ is to enable the
RETURN_FAILURE error code
via your .psrc file and run the program again. It will
then issue diagnostic messages every time a system call fails, including the
memory allocation routines.
EXPR_DANGLING
EXPR_UNINIT_PTR
|