HP Code Advisor Diagnostics Reference Guide (5900-1865, July 2011)
If you are assigning NULL to a pointer, make sure that no path in the program can dereference
that pointer before it is assigned a different value. For routines that may return a NULL, check or
assert that the value returned is not NULL. char * pc = malloc(20); if (pc != NULL) strcpy(pc, "hello");
or: char * pc = malloc(20); assert(pc != NULL); strcpy(pc, "hello");
Reference:
2.164 20201 Memory leak is detected
Cause:
A memory leak is detected for the memory allocation done at the location mentioned in the message.
Example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int leak1 (int k)
{
char *p = malloc (k); // LINE 6
strcpy (p, "hello");
printf ("%s", p);
return 0;
}
"memleak.c", line 6, procedure leak1: warning #20201-D: Memory
leak is detected.
Action:
Make sure that the pointer allocated at the location mentioned in the diagnostic is freed correctly.
Reference:
2.165 20202 Allocated memory may potentially be leaked %s
Cause:
A memory leak may occur for the memory allocation done at the location mentioned in the message.
It is possible that the memory may be leaked along one of the paths from the allocation site.
Example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int* leak2(int k, char* fname)
{
FILE* f;
int *p = (int*) malloc(k); // LINE 7
if (p == 0)
return 0;
if (k > 10)
return 0; // LINE 11
return p;
}
"memleak.c", line 7, procedure leak2: warning #20202-D: Allocated memory
may potentially be leaked (at line 11)
Action:
Make sure that the pointer allocated at the location mentioned in the diagnostic, is freed correctly
along all paths. For example, in the above case, if (k > 10), we return 0 without freeing p. After
this point, p is unreachable from other pointers. Hence it is a potential memory leak along this
point.
Reference:
2.166 20203 Potential out of scope use of local %s %s
Cause:
74 Diagnostics Details