HP Code Advisor Diagnostics

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:
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:
20203 Potential out of scope use of local %s %s
Cause:
A local variable's address is being returned and dereferenced in the caller, or allocated memory
is being returned and used in the caller or a variable defined in the inner scope is being accessed
indirectly in the enclosing scope. Use of local variable outside its scope can lead to unexpected
behavior.
76 Diagnostics Details