这会导致内存泄漏吗? [英] Would this cause a memory leak?

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

问题描述

大家好,


请温柔,因为我是C newby :-)。我想知道这段代码是否会泄漏内存:


somestring =(char *)malloc(initialsize + 1);

....

somestring =(char *)realloc(somestring,newsize + 1);

....

somestring = somestring + moveforward;

....

免费(somestring);

谢谢,Alex

Hello guys,

Please, be gentle, as I am a C newby :-). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
....
somestring = (char *)realloc(somestring, newsize + 1);
....
somestring = somestring + moveforward;
....
free(somestring);
Thanks, Alex

推荐答案

al ******* *****@yahoo.com 写道:

大家好,


请,温柔,因为我是C newby :-)。我想知道这段代码是否会泄漏内存:


somestring =(char *)malloc(initialsize + 1);

...

somestring =(char *)realloc(somestring,newsize + 1);

...

somestring = somestring + moveforward;

...

free(somestring);
Hello guys,

Please, be gentle, as I am a C newby :-). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
...
somestring = (char *)realloc(somestring, newsize + 1);
...
somestring = somestring + moveforward;
...
free(somestring);



实际上不久前问了同样的问题 - 请参阅Google

群组内容< http://tinyurl.com/ yquy75> ;.


摘要 - 它不会泄漏,它会破坏。

Virtually the same question was asked not long ago - see the Google
Groups stuff here <http://tinyurl.com/yquy75>.

Summary - it won''t leak, it will break.


al ************ @ yahoo.com 写道:
al************@yahoo.com wrote:

大家好,


请温柔,因为我是C newby :-)。我想知道这段代码是否会泄漏内存:


somestring =(char *)malloc(initialsize + 1);

...

somestring =(char *)realloc(somestring,newsize + 1);
Hello guys,

Please, be gentle, as I am a C newby :-). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
...
somestring = (char *)realloc(somestring, newsize + 1);



由于realloc在失败时返回NULL,*但是*保留原始内存

不变,你应该总是保存原始块的地址之前

试图重新分配它。否则就无法访问它,

如果realloc用0覆盖地址。

Since realloc returns NULL on failure, *but* leaves the original memory
untouched, you should always save the address of your original block before
trying to reallocate it. Otherwise there''ll be no way to get access to it,
if realloc overwrites the address with 0.


...

somestring = somestring + moveforward;

...

free(somestring);
...
somestring = somestring + moveforward;
...
free(somestring);



是的。 free只能获取之前调用返回的值

malloc / realloc / calloc。任何其他值都是未定义

行为的必然方法,它不一定是内存泄漏。


另请注意,转换malloc的返回值/ realloc / calloc在C中不是
,这样做可以隐藏某些错误。

Yes. free can take only a value returned by a previous call to
malloc/realloc/calloc. Anyother value is a sure-fire way towards undefined
behaviour, which need not necessarily be a memory leak.

Note also that casting the return value of malloc/realloc/calloc is not
necessary in C, and doing so can hide certain errors.


干杯。我现在意识到我发布的代码质量太差了!


我也注意到你建议的链接,这很糟糕:


char * p =(char *)malloc(9);


首选方式是什么?


谢谢,Alex

Cheers. I now realise the awful quality of my posted code!

I''ve also noticed in the link you suggested, that this is bad:

char *p = (char *)malloc(9);

What would be the prefered way?

Thanks, Alex


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

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