内部指针作为参数传出时内存泄漏 [英] Memory leak when internal pointer passed out as parameter

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

问题描述

你好,


我有以下现有代码。而且有内存泄漏。任何人都知道

怎么搭车呢? function foo已在数千个地方使用过,

签名不允许更改。


提前致谢,


my_struc * foo1()

{

my_struc * tmp;


tmp =(my_struc *)calloc(1 ,sizeof(my_struc));


返回tmp;

}


void main()

{

my_struc * mainPtr;


mainPtr = foo1();


免费( mainPtr);

}

Hello,

I have following existing code. And there is memory leak. Anyone know
how to get ride of it? function foo has been used in thousands places,
the signature is not allowed to change.

Thanks in advance,

my_struc * foo1( )
{
my_struc * tmp;

tmp = (my_struc *)calloc(1, sizeof(my_struc));

return tmp;
}

void main()
{
my_struc *mainPtr;

mainPtr = foo1();

free(mainPtr);
}

推荐答案

" Mike" < ma ***** @ gmail.comwrites:
"Mike" <ma*****@gmail.comwrites:

我有以下现有代码。而且有内存泄漏。任何人都知道

怎么搭车呢? function foo已在数千个地方使用,

签名不允许更改。
I have following existing code. And there is memory leak. Anyone know
how to get ride of it? function foo has been used in thousands places,
the signature is not allowed to change.



你的代码中没有可见的内存泄漏,尽管有一个

无效的main()声明和一个不必要的演员。

-

C有它的问题,但从头开始设计的语言也有一些,

我们知道C'的问题。

--Barne Stroustrup

There''s no memory leak visible in your code, although there''s an
invalid declaration of main() and an unnecessary cast.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C''s problems."
--Bjarne Stroustrup


文章< 11 ********** ************@n59g2000hsh.googlegroups .com> ;,

Mike< ma ***** @ gmail.comwrote:
In article <11**********************@n59g2000hsh.googlegroups .com>,
Mike <ma*****@gmail.comwrote:

>我有以下现有代码。而且有内存泄漏。任何人都知道如何驾驭它?
>I have following existing code. And there is memory leak. Anyone know
how to get ride of it?



你确定它是内存泄漏,而不是内存

碎片问题吗?

- -

一切都是虚荣心。 - 传道书

Are you certain that it is a memory leak, and not a memory
fragmentation problem?
--
All is vanity. -- Ecclesiastes


4月3日下午2:29,rober ... @ ibd.nrc-cnrc.gc.ca(Walter Roberson)

写道:
On Apr 3, 2:29 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:

文章< 1175623345.081280.166 ... @ n59g2000hsh.googlegroups .com> ;,


Mike< mail ... @ gmail.comwrote:
In article <1175623345.081280.166...@n59g2000hsh.googlegroups .com>,

Mike <mail...@gmail.comwrote:

我有以下现有代码。而且有内存泄漏。任何人都知道

怎么搭车呢?
I have following existing code. And there is memory leak. Anyone know
how to get ride of it?



您确定它是内存泄漏,而不是内存

碎片问题吗?

- -

一切都是虚荣心。 - Ecclesiastes


Are you certain that it is a memory leak, and not a memory
fragmentation problem?
--
All is vanity. -- Ecclesiastes



Rational Purify检查代码,并在我们分配内存时报告foo1

的内存泄漏。我认为当foo1返回时,编译器将分配一个新的内存块。然后在foo1中分配的内存将永远保留在系统堆中。但是我没有

方式来验证它。

Rational Purify checked the code, and reported memory leak on foo1
when we allocate memory. I assum e that the compiler will allocate a
new block of memory when foo1 returns. Then the memory allocated
within foo1 will remain in the system heap forever. However I have no
way to verify it.


这篇关于内部指针作为参数传出时内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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