Re: Memory Leak Explanation
From: marbac (marbac_at_chello.at)
Date: 05/06/04
- Next message: marbac: "Re: Memory Leak Explanation"
- Previous message: Robert W Hand: "Re: problem with windows code"
- In reply to: pdi: "Memory Leak Explanation"
- Next in thread: marbac: "Re: Memory Leak Explanation"
- Reply: marbac: "Re: Memory Leak Explanation"
- Reply: Karl Heinz Buchegger: "Re: Memory Leak Explanation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: marbac: "Re: Memory Leak Explanation"
- Previous message: Robert W Hand: "Re: problem with windows code"
- In reply to: pdi: "Memory Leak Explanation"
- Next in thread: marbac: "Re: Memory Leak Explanation"
- Reply: marbac: "Re: Memory Leak Explanation"
- Reply: Karl Heinz Buchegger: "Re: Memory Leak Explanation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|