Hi, On Monday 19 June 2006 23:34, Jerry Feldman wrote:
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; }
I see. I was expecting that my testcase would be considered as "not freed but still reachable", whereas "definite lost" would be something like: int main() { int *iptr = malloc(...); iptr = NULL; return 0; } Often I dynamically allocate memory that I know it is going to be used until a program terminates. Though I'll keep pointers to these memory (thus, reachable from main()), I don't bother to explicitly free() these pointers when the program is about to terminate. Afterall, after the program terminates, its address space will be reclaimed by the system. -- Regards, Verdi