Jerry Feldman wrote:
On Monday 19 June 2006 10:56 am, Verdi March wrote:
Hi,
On Monday 19 June 2006 22:09, Jerry Feldman wrote:
No. That is because the pointer is available in main. If you allocate memory inside of a function, and return from that function without freeing that memory or without providing a pointer to that memory, that is unreachable.
I tried the above with a simpler testcase:
int main() { int *iptr iptr (int *) malloc(sizeof(int)); return 0; }
and valgrind still detect a memory leak. Obviously I hold a pointer (iptr) to the allocated memory, or do I misunderstand your explanation?
This is a memory leak because you return from main() without freeing memory. The following code is NOT a memory leak: int main() { int *iptr iptr (int *) malloc(sizeof(int)); free(iptr); return 0;
However the following reports the block as "still reachable" #include <stdlib.h> static int *iptr; int main() { iptr = (int *)malloc(sizeof (int)); return 0; } As commented by Phil Betts, valgrind checks the state of memory AFTER main() exits.