内存泄漏相关问题 [英] Memory Leaks related question

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

问题描述



strcpy(myString,foo())是否会出现内存泄漏???


#include< string.h> ;

#include< stdlib.h>

char * foo()

{

char * str = malloc(200);


if(str!= NULL)

strcpy(str," qwerty");

返回str;

}


int main(int argc,char * argv [])

{

char * myString = malloc(200);


/ *******这里会发生内存泄漏吗? ******* /

strcpy(myString,foo());


返回0;

}


Will there be a memory leak at strcpy(myString, foo()) or not???

#include <string.h>
#include <stdlib.h>
char * foo()
{
char *str = malloc(200);

if (str != NULL)
strcpy(str, "qwerty");

return str;
}

int main(int argc, char *argv[])
{
char * myString = malloc(200);

/******* Will a memory leak occures here? *******/
strcpy(myString, foo());

return 0;
}

推荐答案

ramif说:
ramif said:

>

Will在strcpy(myString,foo())或没有内存泄漏???
>
Will there be a memory leak at strcpy(myString, foo()) or not???



是的。


-

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

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

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

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

Yes.

--
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


ramif写道:
ramif wrote:

>

Will在strcpy(myString,foo())或者没有内存泄漏???


#include< string.h>

#include< ; stdlib.h>


char * foo()

{

char * str = malloc(200);


if(str!= NULL)

strcpy(str," qwerty");


return str;

}


int main(int argc,char * argv [])

{

char * myString = malloc(200);


/ *******这里会发生内存泄漏吗? ******* /

strcpy(myString,foo());


返回0;

}
>
Will there be a memory leak at strcpy(myString, foo()) or not???

#include <string.h>
#include <stdlib.h>
char * foo()
{
char *str = malloc(200);

if (str != NULL)
strcpy(str, "qwerty");

return str;
}

int main(int argc, char *argv[])
{
char * myString = malloc(200);

/******* Will a memory leak occures here? *******/
strcpy(myString, foo());

return 0;
}



嗯,是的,很明显。你已经完成了两次内存分配,而且没有任何一个可以免费使用任何一个内存。当然你有内存泄漏。


你也没有费心检查第一次分配是否成功。

如果任一分配失败,行为您的代码未定义,

因为strcpy()标准定义的行为只有

,这意味着如果第二个参数指向以null结尾的字符串,并且

第一个参数指向一个可写数组足够长的时间来保存该字符串的副本
,包括它的终止空字符。只有少数例外情况,标准通常不需要标准库

函数来检查空指针参数并安全地处理它们。

Well, yes, obviously. You''ve done at two memory allocations, and didn''t
bother free()ing either one. Of course you have a memory leak.

You also didn''t bother checking whether the first allocation succeeded.
If either allocation fails, the behavior of your code is undefined,
because the behavior defined by the standard for strcpy() only has
meaning if the second argument points to a null-terminated string, and
the first argument points to a writable array long enough to hold a copy
of that string, including it''s terminating null character. With only a
few exceptions, the standard does not normally require standard library
function to check for null pointer arguments and handle them safely.


好吧那么,他们是一个解决内存泄漏问题的方法

我的程序(如下所示)?



ramif写道:
Ok then, is their a way to solve the memory leak problem with respect to
my program (shown below)?


ramif wrote:

>

strcpy(myString,foo())是否存在内存泄漏???


#include< string.h>

#include< stdlib.h>


char * foo()

{

char * str = malloc(200);


if(str!= NULL )

strcpy(str," qwerty");


返回str;

}

int main(int argc,char * argv [])

{

char * myString = malloc(200);

/ *******这里会发生内存泄漏吗? ******* /

strcpy(myString,foo());


返回0;

}
>
Will there be a memory leak at strcpy(myString, foo()) or not???

#include <string.h>
#include <stdlib.h>
char * foo()
{
char *str = malloc(200);

if (str != NULL)
strcpy(str, "qwerty");

return str;
}

int main(int argc, char *argv[])
{
char * myString = malloc(200);

/******* Will a memory leak occures here? *******/
strcpy(myString, foo());

return 0;
}


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

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