Memory Leak Explanation

From: pdi (precipice_removethis_at_henzel._.takethisout_..org)
Date: 05/05/04


Date: Wed, 05 May 2004 13:31:00 -0700

Hello All

I have been away from C and C++ for a bit now but having to pick it up
again to do some work on mobile devices. I had a few questions that
came to mind while brushing up on the language again.

I saw this article in the May 2003 version of Linux magazine
discussing Valgrind. In it they had the following example and said
that Valgrind identifies this program as having a memory leak. When
you set the pointer to the second block allocated, the original 512
allocated is lost.

My question is what would be the best way to do this so as to avoid
the mem leak.

Thanks

JPH

Linux Mag Code Sample 2003 May

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *prtr1, *prtr2;
int i;
prtr1 = (char *) malloc (512);
prtr2 = (char *) malloc (512);
prtr2 = ptr1;
free(prt1);
free(ptr2);

}



Relevant Pages

  • Re: How does "new" work in a loop?
    ... freeware program that does this, but it requires all input and output ... My program seems to work fine, but I'm wondering about this loop: ... fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length); ... Unlikely that you are creating a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Why is this a memory leak? (Valgrind / Linux)
    ... I'm hunting down a memory leak reported by Valgrind. ... valgrind doesn't report any memory leaks. ...
    (comp.unix.programmer)
  • [PATCH 1/3] Audit: break up execve argument lists into multiple records
    ... The limit is currently around 7.5k of arguments as userspace ... memory leak on a malloc failure code path. ... making it harder on the kernel to have the memory it needs. ... int audit_n_rules; ...
    (Linux-Kernel)
  • Re: Interesting coding idea
    ... >Dave Vandervies wrote: ... That would be this valgrind (first hit on Google)? ... int *foo ...
    (comp.lang.c)
  • Re: Interesting coding idea
    ... >Dave Vandervies wrote: ... That would be this valgrind (first hit on Google)? ... int *foo ...
    (comp.programming)