c++ - 这段代码会泄漏内存吗? (引用,新的,但没有删除)

标签 c++ memory-leaks

<分区>

Possible Duplicate:
Does using references instead of pointers, resolve memory leaks in C++?

当我问这个问题时

Does using references instead of pointers, resolve memory leaks in C++?

出现一个新问题,我在这篇文章中提出。

这段代码会泄漏内存吗?

class my_class
{
  ...
};

my_class& func()
{
  my_class* c = new my_class;
  return *c;
}

int main()
{
  my_class& var1 = func();

  // I think there is no memory leak.
  return 0;
}

最佳答案

是的,它确实会泄漏内存。由 new 创建的所有内容都必须由 delete 销毁。您的代码中有 new,但没有 delete。这立即意味着 newed 内存泄漏。

关于c++ - 这段代码会泄漏内存吗? (引用,新的,但没有删除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6784152/

相关文章:

c++ - 主游戏循环中的时间计算

javascript - 什么是使用 getImageData、javascript、HTML5 canvas 导致内存泄漏

.net - 什么样的泄漏会导致 WCF 中的 TimeBoundedCache.ExpirableItem 实例泄漏?

c++ - 没有动态内存的内存泄漏

cocoa - CGImageSourceRef 内存泄漏

c++ - 我在循环中遇到问题无法正常工作

c++ - PVS-Studio 不处理头文件

c++ - 五规则和隐式删除函数

c++ - 带有输入无符号字符缓冲区 C++ 的 DeviceIoControl

c - 从函数返回 xmlChar 后内存泄漏