Re: Memory leak
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/16/04
- Next message: Kutty Banerjee: "Re: Memory leak"
- Previous message: Victor Bazarov: "Re: Interview Questions"
- Next in thread: Kutty Banerjee: "Re: Memory leak"
- Maybe reply: Kutty Banerjee: "Re: Memory leak"
- Maybe reply: Ian: "Re: Memory leak"
- Maybe reply: Rolf Magnus: "Re: Memory leak"
- Maybe reply: Anil Mamede: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: John Harrison: "Re: Memory leak"
- Maybe reply: Rolf Magnus: "Re: Memory leak"
- Maybe reply: Karthik: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: John Harrison: "Re: Memory leak"
- Maybe reply: Karl Heinz Buchegger: "Re: Memory leak"
- Maybe reply: Alan Johnson: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: Anil Mamede: "Re: Memory leak"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 16 May 2004 05:44:12 +0100
"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0405152011.6487d244@posting.google.com...
> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following
> questions about memory leak.
> (1).If in my code I only define constructor for my class, and do not
> define destructor, will it cause memory leak?
Depends on the class.
> (2).If in my code I only use "new" to declare new object, and do not
> use "delete", will it cause memory leak?
Yes.
Its a very simple rule, nothing to do with constructors or destructors. When
your program runs every new allocates some memory, if you don't do a delete
for the same memory then you have a memory leak.
>
> For example, in the following code:
>
> void class1::function1()
> {
> class2 *r1;
> class2 *r2 = new class2;
> class2 *rr[20];
>
> ......
>
> function2(rr);
>
> ......
> //r1 and r2 are also used in function1().
>
> }
>
> In the above code, I have two classes and I define constructor for the
> two classes and do not define destructor.
That's irrelevant.
> In function1(), I declare
> two pointers of class2 and an array of pointer of class2. The array rr
> is used to bring back values from function2(). For r2, I do not use
> "delete".
> Will r1, r2 and the array rr[] cause memory leak?
Where are the deletes? There are no deletes so there are memory leaks all
over the place.
> The two pointers, r1 and r2, and array rr[] are local variables, when
> the code exits function1(), these local variables should be released
> automatically.
> Am I right?
Wrong. The variables destructed and the memory they occupy is 'released',
BUT the memory they might be pointing to is not released.
void f()
{
X x;
X* xp = new X();
...
}
When you get to the end of this function, the memory for x and xp are both
released. That has nothing to do with the memory pointed to by xp, which is
a completely different thing. The memory for a pointer and the memory that
it points to are not the same thing.
It's very simple, every new must be matched by a delete.
john
- Next message: Kutty Banerjee: "Re: Memory leak"
- Previous message: Victor Bazarov: "Re: Interview Questions"
- Next in thread: Kutty Banerjee: "Re: Memory leak"
- Maybe reply: Kutty Banerjee: "Re: Memory leak"
- Maybe reply: Ian: "Re: Memory leak"
- Maybe reply: Rolf Magnus: "Re: Memory leak"
- Maybe reply: Anil Mamede: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: John Harrison: "Re: Memory leak"
- Maybe reply: Rolf Magnus: "Re: Memory leak"
- Maybe reply: Karthik: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: John Harrison: "Re: Memory leak"
- Maybe reply: Karl Heinz Buchegger: "Re: Memory leak"
- Maybe reply: Alan Johnson: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: JKop: "Re: Memory leak"
- Maybe reply: Anil Mamede: "Re: Memory leak"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|