动态内存分配和内存泄漏...... [英] Dynamic memory allocation and memory leak...

查看:96
本文介绍了动态内存分配和内存泄漏......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我碰巧遇到这篇令人兴奋的鼓舞人心的文章关于

本网站的内存泄漏:

http://www.embedded.com/story/OEG20020222S0026

在本文中,作者提到:

在代码中的某一点,您可能不确定是否不再需要特定的

块。如果你释放()这段内存,但是

继续访问它(可能通过第二个指向相同的

内存),你的程序可能会完美运行,直到那个特别是

一块内存被重新分配给程序的另一部分。然后

程序的两个不同部分将继续写入每个

其他'的数据。如果你决定不释放内存,理由是

它可能仍在使用中,那么你可能无法获得另一个机会

来释放它(因为所有指针都是如此)块可能已超出范围

或被重新分配到其他地方)。在这种情况下,程序逻辑

不会受到影响。但是如果泄漏内存的代码片段定期访问,那么泄漏将趋于无穷大,因为程序的执行时间会增加。


我无法明确第二句:

如果你释放()这块内存,但继续访问它可能是
(可能通过指向同一内存的第二个指针),你的程序可能会完美运行,直到那个特定的内存是

重新分配到了程序的另一部分。

我也无法得到超出范围的指针如果我不上
释放上面段落中提到的内存?可以有一个

给我这个吗?


我的问题是如何释放内存并使用第二个
指针访问它?我的理解是当我执行malloc时,我获得了对该内存的独占访问权限

,除非我释放它,它不适用于一些

one。如果我的理解是正确的,我没有看到第二个

指针可以访问它的方式。我的意思是说我的理解是当通过malloc分配内存时,返回的指针完全是

访问那个地方。其他一些指针如何有机会指向

到同一个位置(除非程序员明确知道

地址并分配给指针)?


甚至有一段时间bak on MARCH第8个确切地说,对于我对动态

分配的查询,一些专家已经在这篇

文章中说过相同的观点(google forsubba) RAYAN"在这个小组中,你将得到我的

查询)。


如果有人可以向我明确这样做,那将是有帮助的/>
在开发内存约束应用程序时要小心。


查找所有回复的内容并给予高级谢谢,

问候,

s.subbarayan

Dear all,
I happen to come across this exciting inspiring article regarding
memory leaks in this website:

http://www.embedded.com/story/OEG20020222S0026

In this article the author mentions:
"At a certain point in the code you may be unsure if a particular
block is no longer needed. If you free() this piece of memory, but
continue to access it (probably via a second pointer to the same
memory), your program may function perfectly until that particular
piece of memory is reallocated to another part of the program. Then
two different parts of the program will proceed to write over each
other''s data. If you decide to not free the memory, on the grounds
that it may still be in use, then you may not get another opportunity
to free it (since all pointers to the block may have gone out of scope
or been reassigned to point elsewhere). In this case the program logic
will not be affected. But if the piece of code that leaks memory is
visited on a regular basis, the leak will tend towards infinity, as
the execution time of the program increases. "

I am not able to be clear on the second sentence:
"If you free() this piece of memory, but continue to access it
(probably via a second pointer to the same memory), your program may
function perfectly until that particular piece of memory is
reallocated to another part of the program."
I am also not able to get "the pointers to go out of scope" if I dont
free the memory as mentioned in the above paragraph?Can some one
enlight me on this?

My question is how can you free a memory and access it with second
pointer?My understanding was when I do malloc,I get exclusive access
to that piece of memory and unless I free it,its not available to some
one.If my understanding is correct,I dont see a way where a second
pointer can access it.I mean to say that my understanding is when
memory is allocated via malloc,the pointer returned is exclusively
accessing that place.How can some other pointer get a chance to point
to same location(unless the programmer explicitly comes to know the
address and assigns to a pointer)?

Even some time bak on MARCH 8th exactly,to my query on dynamic
allocation,some experts have voiced the same views said in this
article(google for "subbarayan" with in this group and u will get my
query).

It will be helpful if someone can make this clear to me so that I can
be careful while developing memory constraint applications.

Looking faward for all your replys and advanced thanks for the same,
Regards,
s.subbarayan

推荐答案

s.subbarayan写道:
s.subbarayan wrote:
我的问题你是如何释放内存并用第二个指针访问它的?


char * p1,* p2;

p1 = malloc(SOME_SIZE);

p2 = p1;

* p2 = 1; /* 好的。通过p1访问内存;现在* p1和* p2都持有

值1 * /

* p2 = 2; /* 好的。通过p2访问内存;现在* p1和* p2都持有

值2 * /

免费(p1);

* p2 = 3; / *砰! p2正在访问已经通过p1释放的内存* /

我的理解是当我执行malloc时,我获得了对该内存的独占访问权限,除非我释放它,否则它不可用对某些人来说。


True。

如果我的理解是正确的,我没有看到第二个指针可以访问它的方式。
My question is how can you free a memory and access it with second
pointer?
char *p1, *p2;
p1 = malloc(SOME_SIZE);
p2 = p1;
*p2 = 1; /* Okay. Access memory via p1; now *p1 and *p2 both hold the
value 1*/
*p2 = 2; /* Okay. Access memory via p2; now *p1 and *p2 both hold the
value 2 */
free(p1);
*p2 = 3; /* Bang! p2 is accessing memory that has been freed via p1 */
My understanding was when I do malloc,I get exclusive access
to that piece of memory and unless I free it,its not available to some
one.
True.
If my understanding is correct,I dont see a way where a second
pointer can access it.




如果第二个指针属于你,那么问题出在哪里?

Christian



If the second pointer belongs to you, then where''s the problem?
Christian


2005年3月17日22:28:10 -0800,s.subbarayan

< s _ ********** @ rediffmail.com>写道:
On 17 Mar 2005 22:28:10 -0800, s.subbarayan
<s_**********@rediffmail.com> wrote:
亲爱的,
我碰巧在这个网站上遇到过关于内存泄漏这篇令人兴奋的鼓舞人心的文章:


网站不应该有内存泄漏,它们会导致服务器崩溃。哦,对不起,那不是你的意思吗?
http:/ /www.embedded.com/story/OEG20020222S0026

在本文中,作者提到:
在代码中的某个点,您可能不确定某个特定的<不再需要阻止。如果你释放()这段内存,但是
继续访问它(可能通过第二个指向相同内存的指针),你的程序可能会完美运行,直到那个特定的内存被重新分配到该计划的另一部分。然后程序的两个不同部分将继续写出每个
其他的数据。如果您决定不释放内存,理由是它可能仍在使用中,那么您可能无法获得另一个机会
来释放它(因为所有指向该块的指针可能已经消失了范围
或被重新分配到其他地方)。在这种情况下,程序逻辑不会受到影响。但是如果定期访问泄漏内存的代码段,则泄漏将趋于无穷大,因为程序的执行时间会增加。 "


是的。

我无法清楚第二句:
如果你释放()这段记忆,但是继续访问它(可能通过指向同一内存的第二个指针),你的程序可能会完美运行,直到该特定的内存被重新分配给程序的另一部分。 ;


...

char * p;

char * q;

...

p = malloc(size); / * p指向内存区域* /

...

q = p; / * q指向相同的内存* /

...

free(p); / *内存可用于重新分配* /

...

* q = 5; / * q仍然指向释放的内存* /

我也无法得到超出范围的指针如果我没有释放上段中提到的记忆?有人可以给我这个吗?


void func(size_t size)

{

char * p = malloc(size);

...

}


内存仍然已分配,但指向的指针现在不可见

where所以你不能释放内存。

我的问题是如何释放内存并用第二个指针访问它?我的理解是当我做malloc时,我获得了独占访问权限
这段记忆


你是什么意思独家访问?你的程序有独占的

访问内存,但是那个程序中你和

之类的指针可以指向ait,如上所述。

除非我释放它,否则它不适用于某些人。如果我的理解是正确的,我没有看到第二个指针可以访问它的方式。我的意思是说我的理解是什么时候
内存通过malloc分配,返回的指针专门访问那个地方。一些其他指针如何有机会指向同一位置(除非程序员明确知道
地址并分配给指针)?
Dear all,
I happen to come across this exciting inspiring article regarding
memory leaks in this website:
There shouldn''t be memory leaks in websites, they will cause the server
to crash. Oh, sorry, that''s not what you meant?
http://www.embedded.com/story/OEG20020222S0026

In this article the author mentions:
"At a certain point in the code you may be unsure if a particular
block is no longer needed. If you free() this piece of memory, but
continue to access it (probably via a second pointer to the same
memory), your program may function perfectly until that particular
piece of memory is reallocated to another part of the program. Then
two different parts of the program will proceed to write over each
other''s data. If you decide to not free the memory, on the grounds
that it may still be in use, then you may not get another opportunity
to free it (since all pointers to the block may have gone out of scope
or been reassigned to point elsewhere). In this case the program logic
will not be affected. But if the piece of code that leaks memory is
visited on a regular basis, the leak will tend towards infinity, as
the execution time of the program increases. "
Yup.
I am not able to be clear on the second sentence:
"If you free() this piece of memory, but continue to access it
(probably via a second pointer to the same memory), your program may
function perfectly until that particular piece of memory is
reallocated to another part of the program."
...
char *p;
char *q;
...
p = malloc(size); /* p points to a memory area */
...
q = p; /* q points to the same memory */
...
free(p); /* memory is available for reallocation*/
...
*q = 5; /* q is still pointing at the freed memory */
I am also not able to get "the pointers to go out of scope" if I dont
free the memory as mentioned in the above paragraph?Can some one
enlight me on this?
void func(size_t size)
{
char *p = malloc(size);
...
}

The memory is still allocated, but the pointer to is is not now visible
anywhere so you can''t free the memory.
My question is how can you free a memory and access it with second
pointer?My understanding was when I do malloc,I get exclusive access
to that piece of memory
What do you mean by "exclusive access"? Your program has exclusive
access to the memory, but as many pointers within that program as you
like can point at ait, as above.
and unless I free it,its not available to some
one.If my understanding is correct,I dont see a way where a second
pointer can access it.I mean to say that my understanding is when
memory is allocated via malloc,the pointer returned is exclusively
accessing that place.How can some other pointer get a chance to point
to same location(unless the programmer explicitly comes to know the
address and assigns to a pointer)?




我认为你是混淆地址(内存区域有一个固定的
地址,和malloc()不能重新分配那个内存,直到它被释放)和

指针变量。指针变量只是变量,你可以将它们分配给相同类型的其他指针变量(甚至是不同的带有铸造的
类型,尽管这很危险并且通常

非便携式),向它们添加和减去整数等等。


如果一次只有一个指针可以指向
特定的内存位置。例如:


#include< stdio.h>

int getLine(char * buff,int length)

{

char * p = buff;

int c;

while(length - > 0&&((c = getchar) ())!=''\ n'')

* p ++ = c;

* p =''\''';

返回p - buff;

}


Chris C



I think you are confusing addresses (the memory area has a fixed
address, and malloc() can''t reassign that memory, until it is freed) and
pointer variables. Pointer variables are just variables, you can assign
them to other pointer variables of the same type (or even of different
types with casting, although that is dangerous and generally
non-portable), add and subtract integers to them, etc.

It would be very unuseful if only one pointer at a time could point to a
particular memory location. For instance:

#include <stdio.h>
int getLine(char *buff, int length)
{
char *p = buff;
int c;
while (length-- > 0 && ((c = getchar()) != ''\n'')
*p++ = c;
*p = ''\0'';
return p - buff;
}

Chris C


" s。 subbarayan"< s _ ********** @ rediffmail.com>在留言中写道


< snip>
"s.subbarayan" <s_**********@rediffmail.com> wrote in message

<snip>
我无法清楚第二句:
如果你释放()这块内存,但继续访问它(可能是通过指向同一内存的第二个指针),你的程序可能会完美运行,直到特定的内存被重新分配到程序的另一部分。
我也无法得到超出范围的指针 ;如果我没有释放内存,如上所述以上段落?有人可以在这上面给我这个吗?


char * p,* q;


p = q = malloc(10);

p + = 11 ; / *< - 超出范围* /





p = malloc(10);

q = realloc(p,15);

printf(" p = 0x%p\\\
q = 0x%p\ n,(void *)p,(void *)q);


其中''p''和''q''可能不再指向同一个mem块,

所以


* p = 0;


是UB。

我的问题是如何释放内存并使用第二个访问它指针?


轻松


#include< stdlib.h>

#include< stdio.h>

#include< string.h>


int main(无效)

{

char * p,* q;


p = q = malloc(10);


strcpy(p,Alloced!);

printf(" p = 0x%p\\\
q = 0x%p\\\
Content of''q'':%s \ n",(void *)p,(void

*)q,q);


free(p);

printf(" p = 0x%p\ nq = 0x%p \ n",(void *)p,(void *)q);

}


我的理解是当我做的时候malloc,我获得了对该内存的独占访问权限,除非我免费使用,
I am not able to be clear on the second sentence:
"If you free() this piece of memory, but continue to access it
(probably via a second pointer to the same memory), your program may
function perfectly until that particular piece of memory is
reallocated to another part of the program."
I am also not able to get "the pointers to go out of scope" if I dont
free the memory as mentioned in the above paragraph?Can some one
enlight me on this?
char *p, *q;

p = q = malloc(10);
p += 11; /* <-- out of scope */

or

p = malloc(10);
q = realloc(p, 15);
printf("p = 0x%p\nq = 0x%p\n", (void *)p,(void *)q);

where ''p'' and ''q'' might not be pointing at the same mem chunk anymore,
so

*p = 0;

is UB.
My question is how can you free a memory and access it with second
pointer?
easy

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

int main(void)
{
char *p, *q;

p = q = malloc(10);

strcpy(p, "Alloced!");
printf("p = 0x%p\nq = 0x%p\nContent of ''q'': %s\n", (void *)p,(void
*)q, q);

free(p);
printf("p = 0x%p\nq = 0x%p\n", (void *)p,(void *)q);
}

My understanding was when I do malloc,I get exclusive access
to that piece of memory and unless I free it,




在上面的代码中,''p''和'' q''指向_same_ malloc''对象,但是在free()调用之后



解除引用''p''或'q''是UB。


-

Tor< torust AT online DOT no>

" To这一天,许多C程序员认为强打字只是意味着键盘上的额外硬盘。 PvdL



In the code above, ''p'' and ''q'' point to the _same_ malloc''ed object, but
after the free() call,
dereferencing ''p'' or ''q'' is UB.

--
Tor <torust AT online DOT no>
"To this day, many C programmers believe that ''strong typing'' just means
pounding extra hard on the keyboard". PvdL


这篇关于动态内存分配和内存泄漏......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆