为什么要使用new和delete? [英] Why use new and delete at all?

查看:539
本文介绍了为什么要使用new和delete?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我想知道为什么我应该使用新的和删除?它可以导致问题(内存泄漏),我不明白为什么我不应该只是初始化一个变量没有new运算符。有人可以解释一下吗?

I'm new to C++ and I'm wondering why I should even bother using new and delete? It can cause problems (memory leaks) and I don't get why I shouldn't just initialize a variable without the new operator. Can someone explain it to me? It's hard to google that specific question.

推荐答案

出于历史和效率原因,C ++(和C)内存管理是显式和手动的。

For historical and efficiency reasons, C++ (and C) memory management is explicit and manual.

有时, 调用堆栈(例如使用 VLA alloca(3))。但是,这并不总是可能,因为

Sometimes, you might allocate on the call stack (e.g. by using VLAs or alloca(3)). However, that is not always possible, because


  1. 堆栈大小有限(取决于平台,几千字节或几兆字节) 。

  2. 内存需求不总是 FIFO LIFO 。它确实发生,你需要分配内存,这将在执行过程中释放(或变得无用),特别是因为它可能是一些函数的结果(和调用者 - 或其调用者将释放该内存)。

  1. stack size is limited (depending on the platform, to a few kilobytes or a few megabytes).
  2. memory need is not always FIFO or LIFO. It does happen that you need to allocate memory, which would be freed (or becomes useless) much later during execution, in particular because it might be the result of some function (and the caller - or its caller - would release that memory).

您一定要阅读垃圾回收动态内存分配。在一些语言(Java,Ocaml,Haskell,Lisp,....)或系统中,提供GC,并且负责释放无用(更准确地说是不可达的)数据的存储器。另请参阅弱引用。请注意,大多数 GC 需要扫描调用堆栈的本地指针。

You definitely should read about garbage collection and dynamic memory allocation. In some languages (Java, Ocaml, Haskell, Lisp, ....) or systems, a GC is provided, and is in charge of releasing memory of useless (more precisely unreachable) data. Read also about weak references. Notice that most GCs need to scan the call stack for local pointers.

请注意,有可能但很难有非常高效的垃圾收集器(但通常不是在C ++中)。对于某些 程序,使用代数复制GC的Ocaml比具有显式内存管理的等价C ++代码快。

Notice that it is possible, but difficult, to have quite efficient garbage collectors (but usually not in C++). For some programs, Ocaml -with a generational copying GC- is faster than the equivalent C++ code -with explicit memory management.

显式管理内存有一个优点(在C ++中很重要),你不需要为不需要的东西付费。

Managing memory explicitly has the advantage (important in C++) that you don't pay for something you don't need. It has the inconvenience of putting more burden on the programmer.

在C或C ++中,有时可以考虑使用 Boehm的保守的垃圾收集器。使用C ++,您可能有时需要使用自己的分配器,而不是默认 std :: allocator 。另请参阅智能指针引用计数 std :: shared_ptr std :: unique_ptr std :: weak_ptr RAII 成语,以及规则三(在C ++中,成为规则5)。最近的智慧是避免显式的 new delete (例如通过使用标准容器和智能指针)。

In C or C++ you might sometimes consider using the Boehm's conservative garbage collector. With C++ you might sometimes need to use your own allocator, instead of the default std::allocator. Read also about smart pointers, reference counting, std::shared_ptr, std::unique_ptr, std::weak_ptr, and the RAII idiom, and the rule of three (in C++, becoming the rule of 5). The recent wisdom is to avoid explicit new and delete (e.g. by using standard containers and smart pointers).

注意最困难的情况

在Linux和其他系统上, valgrind 是一个用于寻找内存泄漏的有用工具。

On Linux and some other systems, valgrind is a useful tool to hunt memory leaks.

这篇关于为什么要使用new和delete?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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