Re: Memory Leak Explanation

From: marbac (marbac_at_chello.at)
Date: 05/06/04


Date: Thu, 06 May 2004 09:31:30 GMT

Hi,

> prtr1 = (char *) malloc (512);
> prtr2 = (char *) malloc (512);
First you allocate 512 bytes for ptr1 on the heap
After this you allocate !another! 512 bytes for ptr2 on the heap.

> prtr2 = ptr1;

An then follows the mistake ... you move away ptr2 from the 512 bytes
allocated for ptr2. There is no other pointer pointing to this
"ptr2-512bytes". You are not able to delete (free) this 512 bytes from
now on (memory leak).

> free(prt1);

Here you delete (free) the 512 bytes of ptr1 and ptr2.

> free(ptr2);

You deleted (free) the 512 bytes of ptr2 yet because you deleted the 512
bytes
of ptr1 -> runtime error

But i also have a question.
If i allocate an array, i have to delete it with delete [] instead of
delete.
Is this not necessary with free?

regards marbac



Relevant Pages

  • Re: Question about memory allocation??
    ... char *ptr1; ... char arr; ... char *ptr2; ... if you want to allocate an array: ...
    (comp.lang.c)
  • Re: Huge pages and small pages. . .
    ... >> what looks like contiguous memory and away you go. ... you need to have them cached in the TLB; if the TLB runs out of ... > low end of the heap, until someone figures out a way to tell the system ... When you allocate memory, the kernel just marks a promised ...
    (Linux-Kernel)
  • Re: Struct inside class
    ... The compiler figures computes the ... including anything derived from "Object": Heap pointer ... memory leaks and why the .NET good garbage collector is required. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Re: Struct inside class
    ... From the previous discussion, we know that i1 is a member variable of a strunct in a class, so it's stored on the heap. ... but inside the allocated space for the reference type instance. ... memory leaks and why the .NET good garbage collector is required. ... If GC_ALLOCATE can't allocate the requested ...
    (microsoft.public.dotnet.framework)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)