![]() |
'#include <stdio.h>
'#include <malloc.h>
static char *helloWorld = "Hello, World";
int main()
{
char *myString = malloc( strlen(helloWorld) );
strncpy(myString, helloWorld, 12);
printf("%s\n", myString);
}
% purify cc hello_world.c
Because the strncpy() call copied only the first 12 characters of the string and did not copy the ending '\0', printf scanned off the end of the block of malloced memory. Purify shows you the code that caused the error and where the block of memory was allocated.
When the function main() returns, all of its variables in it go out of scope. In this case, myString goes out of scope, and no pointer is left pointing at the malloced piece of memory. So Purify reports the block of memory as leaked.