且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

内存泄漏

更新时间:2022-05-21 15:32:28



" John" <乔********* @ yahoo.com>在留言中写道

news:c3 ************************** @ posting.google.c om ...

"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
大家好:

当我运行我的代码时,我发现代码使用的内存保持增加。
我有一台PC运行Debian linux的2G RAM。代码执行完成后,代码消耗1.5G内存。但我不认为它需要如此多的记忆。大约500M内存应该足够了。我有以下关于内存泄漏的问题。
(1)。如果在我的代码中我只定义了我的类的构造函数,并且没有定义析构函数,它会导致内存泄漏吗?


取决于课程。

(2)。如果在我的代码中,我只使用new声明新对象,并且不使用删除,会导致内存泄漏吗?


是的。


这是一个非常简单的规则,与构造函数或析构函数无关。当你的程序运行时,每个新程序会分配一些内存,如果你没有为同一个内存删除

那么你就会有内存泄漏。


例如,在以下代码中:

void class1 :: function1()
{
class2 * r1;
class2 * r2 = new class2;
class2 * rr [20];

......

function2(rr);
> ......
// r1和r2也用在function1()中。

}

在上面的代码中,我有两个类我定义了两个类的构造函数,并没有定义析构函数。


那是无关紧要的。

在function1()中,我声明了两个指针class2和一个class2指针数组。数组rr
用于从function2()返回值。对于r2,我不使用
delete。
r1,r2和数组rr []会导致内存泄漏吗?


删除在哪里?没有删除所以内存泄漏全部



两个指针,r1和r2,以及数组rr []是局部变量,当
代码退出function1(),这些局部变量应该自动释放。
我是对的吗?
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?




错了。被破坏的变量和它们占用的记忆被''释放'',

但是他们可能指向的记忆没有被释放。


void f( )

{

X x;

X * xp =新X();

...
}


当你到达这个函数的末尾时,x和xp的内存都是

发布。这与xp指向的内存无关,这是一个完全不同的东西。指针的内存和它指向的内存是不一样的。


这很简单,每个新的必须匹配删除。


john



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




" John" &LT;乔********* @ yahoo.com&GT;在留言中写道

news:c3 ************************** @ posting.google.c om ...

"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
大家好:

当我运行我的代码时,我发现代码使用的内存保持增加。
我有一台PC运行Debian linux的2G RAM。代码执行完成后,代码消耗1.5G内存。但我不认为它需要如此多的记忆。大约500M内存应该足够了。我有关于内存泄漏的以下问题。
(1)。如果在我的代码中我只定义了我的类的构造函数,并且没有定义析构函数,它会导致内存泄漏吗? />(2)。如果在我的代码中,我只使用new声明新对象,并且不使用删除,会导致内存泄漏吗?

例如,在以下代码中:

void class1 :: function1()
{
class2 * r1;
class2 * r2 = new class2;
class2 * rr [20];

.. ....

function2(rr);

......
// r1和r2也用在function1()中。 />
}

在上面的代码中,我有两个类,我为两个类定义了构造函数,并没有定义析构函数。在function1()中,我声明了两个class2的指针和一个class2的指针数组。数组rr
用于从function2()返回值。对于r2,我不使用
delete。
r1,r2和数组rr []会导致内存泄漏吗?
两个指针r1和r2,以及数组rr [ ]是局部变量,当代码退出function1()时,这些局部变量应该自动释放。
我是对的吗?

非常感谢。

John
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?
(2).If in my code I only use "new" to declare new object, and do not
use "delete", will it cause 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. 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?
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?

Thanks a lot.

John





签出auto_ptr来自任何标准的c ++ boooks。


kutty


Hi,
check out "auto_ptr" from any standard c++ boooks.

kutty


John写道:
大家好:
我有一台运行Debian linux的2G内存的PC。代码执行完成后,代码消耗1.5G内存。但我不认为它需要如此多的记忆。大约500M内存应该足够了。我有以下关于内存泄漏的问题。
(1)。如果在我的代码中我只定义了我的类的构造函数,并且没有定义析构函数,它会导致内存泄漏吗?


见下一个答案! (2)。如果在我的代码中,我只使用新代码。声明新对象,并且不使用删除,是否会导致内存泄漏?

是的,没有析构函数,你分配的内存是怎样的

析构函数是否已释放?


另一种方法是使用std :: auto_ptr而不是普通指针。


通常情况下,任何带有指针的类都必须有一个析构函数并复制

构造函数(如果没有别的,这会让你考虑通过指针引用的数据的所有权)。


Ian

例如,在以下代码中:

void class1 :: function1()
{
class2 * r1;
class2 * r2 = new class2;
class2 * rr [20];

......

function2 (rr);

......
// r1和r2也用在function1()中。

}

两个类定义构造函数,并且没有定义析构函数。在function1()中,我声明了两个class2的指针和一个class2的指针数组。数组rr
用于从function2()返回值。对于r2,我不使用
delete。
r1,r2和数组rr []会导致内存泄漏吗?
两个指针r1和r2,以及数组rr [ ]是局部变量,当代码退出function1()时,这些局部变量应该自动释放。
我是对的吗?

非常感谢。

John
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?
See next answer! (2).If in my code I only use "new" to declare new object, and do not
use "delete", will it cause memory leak?
Yes, without a destructor, how is the memory you allocated in the
destructor freed?

An alternative is to use std::auto_ptr rather than a plain pointer.

As a rule, any class with pointers must have a destructor and copy
constructor (if nothing else, this makes you think about ownership of
the data you reference through a pointer).

Ian
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. 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?
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?

Thanks a lot.

John