自由();在return()之后;? [英] free(); after return();?

查看:77
本文介绍了自由();在return()之后;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我的情况:


如果我使用malloc:


char * mystring;


mystring =(char *)malloc(80);


返回mystring;

free(mystring);


是mystring的内存然后被释放还是会产生内存泄漏?
内存泄漏?如果是这样,我怎么能阻止这个?


提前谢谢,

Robert

Alright, here''s my situation:

if i use malloc like:

char *mystring;

mystring = (char *)malloc(80);

return mystring;
free(mystring);

is the memory of mystring then freed or does this creates
memory leaks? And if so, how could i prevent this?

Thanks in advance,
Robert

推荐答案

ipo写道:

< snip top-posted crap>
ipo wrote:
<snip top-posted crap>
最后,谁告诉你void main(void)不是有效的C?我建议您获得K& R的副本并阅读它。
ipo
Finally, whoever told you that void main(void) is not valid C? I recommend
you get a copy of K&R and read it.
ipo




我很确定他有。你呢?另外我认为最新的C标准

会帮助你。


我不能相信像你这样的世界存在无知。


失败者。



I''m very sure he has. Have you? Also I think the latest C standard
would help you out.

I can''t believe ignorance such as yours exists in this world.

Loser.


" Robert" < R **** @ hetnet.nl>。在消息中写道

news:bj ********** @ reader11.wxs.nl ...

|是mystring的记忆然后被释放或者是否创造了

|内存泄漏?如果是这样,我怎么能阻止这个呢?

确实如此,因为没有达到免费。


在你的函数之外调用free()就可以了一旦你完成它。它不需要在同一范围内。
"Robert" <R.****@hetnet.nl> wrote in message
news:bj**********@reader11.wxs.nl...
| is the memory of mystring then freed or does this creates
| memory leaks? And if so, how could i prevent this?
it sure does, as free is not reached.

Outside of your function call free() on it once you are done with it. It
doesn''t have to be in the same scope.


此函数不释放mystring的内存,因为函数返回

在到达free(mystring)之前。是否会遇到内存泄漏

取决于内存是否在程序中的其他地方释放。不要将
混淆局部变量mystring的范围与它所指向的物品范围

。您的代码应如下所示:


char * getString(void){

char * mystring;


mystring =(char *)malloc(80 * sizeof(char));

return(mystring);

}


void main(void){

char * myName;


myName = getString();


if( myName!= NULL){

strcpy(myName," Bill Clinton");

printf("%s \ n",myName);

免费(myName);

}

}


当getString()返回时,mystring消失了范围。但请注意,

函数返回的是指向80个字符内存块的指针。在那之后

请注意,我确保main()调用free()来释放内存块

一旦我完成它。


我希望这有帮助

ipo


" Robert" < R **** @ hetnet.nl>。在消息中写道

news:bj ********** @ reader11.wxs.nl ...
The memory of mystring is not freed by this code since the function returns
prior to reaching free(mystring). Whether you will run into memory leaks
depends on whether the memory is freed elsewhere in the program. Don''t
confuse the scope of the local variable mystring with the scope of the thing
it points to. Your code should read as follows:

char *getString(void) {
char *mystring;

mystring = (char *) malloc(80* sizeof(char));
return(mystring);
}

void main(void) {
char *myName;

myName = getString();

if (myName != NULL) {
strcpy(myName, "Bill Clinton");
printf("%s\n", myName);
free(myName);
}
}

When getString() returns, mystring goes out of scope. But note that what the
function returns is a pointer to a 80-character memory chunk. After that
note that I make sure that main() calls free() to release the memory chunk
once I''m done with it.

I hope this helps
ipo

"Robert" <R.****@hetnet.nl> wrote in message
news:bj**********@reader11.wxs.nl...
好吧,这是我的情况:

如果我使用malloc就像:

char * mystring;

mystring =(char *)malloc(80);

返回mystring;
free(mystring);

是mystring的内存然后释放还是这会造成内存泄漏?如果是这样,我怎么能阻止这个?

提前致谢,
Robert
Alright, here''s my situation:

if i use malloc like:

char *mystring;

mystring = (char *)malloc(80);

return mystring;
free(mystring);

is the memory of mystring then freed or does this creates
memory leaks? And if so, how could i prevent this?

Thanks in advance,
Robert



这篇关于自由();在return()之后;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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