内存泄漏.. [英] Memory leaking..

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

问题描述

#include< stdio.h>

#include< string.h>

#include< stdlib.h>

char * strclear(const char * str)

{

char * string;

string =(char *)malloc (strlen(str)+1);

strcpy(string,str);

返回字符串;

}

int main()

{

char * str =" Five pineapples";

char * string;

string = strclear(str);

printf(" string is%s \ n",string);

free(string);

返回0;

}


请参考以上程序查询我的问题..


1.当我使用valgrid运行时,我收到消息为16个字节在1个街区中仍然

可到达。我认为记忆在程序中泄露了。我是对的吗?


2.因为我分配堆内存起始地址的值并且调用

free它没有被释放..为什么?


3.你能不能告诉我怎样才能释放另一个功能动态分配的
的内存。没有使用任何

globel变量?


请帮帮我..

提前致谢,

Ganesh

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

char * strclear(const char *str)
{
char *string;
string=(char *)malloc(strlen(str)+1);
strcpy(string,str);
return string;
}
int main()
{
char *str="Five pineapples";
char *string;
string=strclear(str);
printf ("The string is %s\n",string);
free(string);
return 0;
}

Please refer above program for my question..

1. When i run with valgrid i got message as "16 byts in 1 block still
reachable". I think memory is leaking in the program. Am i right?

2. Since i assign value of heap memory starting address and calling
free it is not freed.. why?

3. Could you any one please tell me how can i free the memory which
has been allocated dynamically by another function. without using any
globel variable?

Please help me ..
Thanks in advance,
Ganesh

推荐答案

gNash写道:
gNash wrote:

#include< stdio.h>

#include< string.h>

#include< stdlib.h>


char * strclear(const char * str)

{

char * string;

string =(char *)malloc(strlen(str)+1);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * strclear(const char *str)
{
char *string;
string=(char *)malloc(strlen(str)+1);



请放弃演员,你也可以初始化字符串

声明。

Please drop the cast and you may as well initialise string where it is
declared.


strcpy(string,str);

返回字符串;

}


int main()

{

char * str ="五个菠萝" ;;

char * string;

string = strclear( str);

printf(字符串是%s \ n,字符串);

免费(字符串);

返回0;

}


请参考以上程序查询我的问题..


1.当我跑步时valgrid我收到消息为16个字节在1个区块中仍然

可到达。我认为记忆在程序中泄露了。我是对的吗?
strcpy(string,str);
return string;
}
int main()
{
char *str="Five pineapples";
char *string;
string=strclear(str);
printf ("The string is %s\n",string);
free(string);
return 0;
}

Please refer above program for my question..

1. When i run with valgrid i got message as "16 byts in 1 block still
reachable". I think memory is leaking in the program. Am i right?



No.

No.


2.因为我分配了堆内存起始地址的值和打电话给

免费它没有被释放..为什么?
2. Since i assign value of heap memory starting address and calling
free it is not freed.. why?



它已被释放。


-

Ian Collins。

It is freed.

--
Ian Collins.


gNash说:
gNash said:

#include< ; stdio.h>

#include< string.h>

#include< stdlib.h>


char * strclear(const char * str)

{

char * string;

string =(char *)malloc(strlen(str)+ 1);

strcpy(string,str);

返回字符串;

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

char * strclear(const char *str)
{
char *string;
string=(char *)malloc(strlen(str)+1);
strcpy(string,str);
return string;
}



更好(或至少不同!):


char * clearstr(const char * str)/ * [1] * /

{

size_t len = strlen(str)+ 1; / * [2] * /

char * string = malloc(len); / * [3] * /

if(string!= NULL)/ * [4] * /

{

memcpy(string, str,len); / * [5] * /

}

返回字符串;

}


[1 ] - str后跟az是保留供外部使用。通过

实现,所以不要在你自己的函数名中使用它。

[2] - 在malloc中嵌入strlen很好,但是如果你选择的话去

memcpy路线(见[5]),你会想要缓存这个值。

[3] - 不需要转换返回值malloc,虽然*在这种情况下*

你的演员没有实际的伤害隐藏,因为你记得

#include< stdlib.h>,所以没有伤害这个场合。

[4] - malloc可能会失败。检查它是否在信任结果之前。

[5] - 如果逻辑是任何指南,那么memcpy执行复制的速度比复制快得多strcpy的。在实践中,它似乎取决于

的实现,在某种程度上取决于字符串的长度。所以这个和上面的准备步骤[2]是我只是简单地提到

供考虑,而不是积极推荐。

Better (or at least different!):

char *clearstr(const char *str) /* [1] */
{
size_t len = strlen(str) + 1; /* [2] */
char *string = malloc(len); /* [3] */
if(string != NULL) /* [4] */
{
memcpy(string, str, len); /* [5] */
}
return string;
}

[1] - str followed by a-z is "reserved for external use" by the
implementation, so don''t use it in your own function names.
[2] - nesting the strlen in the malloc is fine, but if you choose to go the
memcpy route (see [5]), you''ll want to cache this value.
[3] - no need to cast the return value of malloc, although *in this case*
your cast did no actual damage concealment, since you remembered to
#include <stdlib.h>, so no harm done on this occasion.
[4] - malloc can fail. Check that it doesn''t before trusting the result.
[5] - if logic were any guide, it would make sense for memcpy to perform
the copy faster than strcpy. In practice, it seems to depend on the
implementation and to some extent on the length of the string. So this and
the preparatory step [2], above, are the bits that I''m simply mentioning
for consideration, rather than positively recommending.


int main()

{

char * str =" Five pineapples";

char * string;

string = strclear(str);
int main()
{
char *str="Five pineapples";
char *string;
string=strclear(str);



见上面的[1]。

See [1] above.


printf(" string is%s \ n" ;,串);
printf ("The string is %s\n",string);



检查是否为空:


if(string!= NULL)

{

printf("字符串是%s \ n",字符串);

免费(字符串);

}


< snip>

Check for NULL:

if(string != NULL)
{
printf("The string is %s\n", string);
free(string);
}

<snip>


1.当我使用valgrid运行时,我收到消息为16个字节在1个区块中仍然

可到达。我认为记忆在程序中泄露了。我对吗?
1. When i run with valgrid i got message as "16 byts in 1 block still
reachable". I think memory is leaking in the program. Am i right?



你展示的程序没有泄漏。

The program you showed has no leak.


2.因为我分配了堆内存的值地址和电话

免费它没有被释放..为什么?
2. Since i assign value of heap memory starting address and calling
free it is not freed.. why?



该程序没有机制来检测该声明是否正确。

The program has no mechanism to detect whether that claim is correct.


3.你能不能请告诉我如何释放由另一个函数动态分配的
的内存。没有使用任何

globel变量?
3. Could you any one please tell me how can i free the memory which
has been allocated dynamically by another function. without using any
globel variable?



你已经知道怎么做了。你已经做到了。如果valgrind是

就像你在节目中所说的那样表现,那么valgrind是错误的或

你的实现是次优的。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

You already know how to do it. You''ve already done it. If valgrind is
behaving as you say on the program you show, then valgrind is wrong or
your implementation is sub-optimal.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


11月15日下午12:04,Ian Collins< ian-n ... @ hotmail.comwrote:
On Nov 15, 12:04 pm, Ian Collins <ian-n...@hotmail.comwrote:

gNash写道:
gNash wrote:

#include< stdio.h>

#include< string .h>

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


char * strclear(const char * str)

{

char * string;

string =(char *)malloc(strlen(str)+1);
char * strclear(const char *str)
{
char *string;
string=(char *)malloc(strlen(str)+1);



请放弃演员,你也可以初始化字符串

声明。


Please drop the cast and you may as well initialise string where it is
declared.


strcpy(string,str);

返回字符串;

}
strcpy(string,str);
return string;
}


int main()

{

char * str =" Five pineapples";

char * string;

string = strclear(str);

printf(字符串是%s \ n,字符串);

free(string);

返回0;

}
int main()
{
char *str="Five pineapples";
char *string;
string=strclear(str);
printf ("The string is %s\n",string);
free(string);
return 0;
}


请参考以上程序查询我的问题..
Please refer above program for my question..


1.当我使用valgrid运行时,我收到消息为16个字节在1个区块中仍然

可到达。我认为记忆在程序中泄露了。我对吗?
1. When i run with valgrid i got message as "16 byts in 1 block still
reachable". I think memory is leaking in the program. Am i right?



No.


No.


2.因为我分配堆内存起始地址的值并调用

免费它没有被释放..为什么?
2. Since i assign value of heap memory starting address and calling
free it is not freed.. why?



它已被释放。


-

Ian Collins。


It is freed.

--
Ian Collins.



谢谢你Ian Collins ..但是我没有收到来自valgrind的消息,因为所有

堆内存被释放它是说16个字节仍然可以访问

这是什么意思?


请解释我..


Thankyou Ian Collins.. but i not getting message from valgrind as "all
heap memory are freed " it is saying "16 bytes are still reachable "
what it mean?

Please explain me..


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

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