我们应该删除支持参考 [英] Should we delete supporting reference

查看:56
本文介绍了我们应该删除支持参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下任何代码



If I have any code as follow

void foo(BYTE* a, int c)
{
BYTE *c=a;
//do some operations on c 
}





我应该在方法结束时删除c。如果我不删除'c'我的程序中是否有任何内存泄漏?



我尝试过:



i在我的代码中有类似的情况,它每小时泄漏近14 mb



should i delete c at the end of method . if i don't delete ' c ' do i get any memory leak in my program ?

What I have tried:

i have similar scenarios in my code and it is leaking almost 14 mb per hour

推荐答案

引用:

我应该在方法结束时删除c

should i delete c at the end of method

否。

你显示的功能没有分配任何内存,除了在堆栈上分配的指针 c 本身,并且当函数结束时将自动回收。

你不应该删除c指向的值,因为它作为参数传递给函数,可以在函数外部使用。



你应该删除你创建的值只有 new ,甚至在你完成它们时删除它们。

在别处寻找你的内存泄漏!

No.
The function you show doesn't allocate any memory, except for the pointer c itself which is allocated on the stack, and which will be reclaimed automatically when the function ends as a result.
You should not delete the value c points to, as it was passed to the function as a parameter and may be used outside the function.

You should delete values you create via new only, and even then delete them when you are finished with them.
Look elsewhere for your memory leak!


这篇关于我们应该删除支持参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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