Malloc vs原始的新功能 [英] Malloc vs New for Primitives

查看:91
本文介绍了Malloc vs原始的新功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解在C ++中针对malloc使用new的好处.但是对于特定情况,例如原始数据类型(非数组)-intfloat等,使用malloc比使用new更快吗?

I understand the benefits of using new against malloc in C++. But for specific cases such as primitive data types (non array) - int, float etc., is it faster to use malloc than new?

尽管如此,如果我们正在分配数组以便可以使用delete[],那么即使对于基元也总是建议使用new.

Although, it is always advisable to use new even for primitives, if we are allocating an array so that we can use delete[].

但是对于非数组分配,我认为对于int不会有任何构造函数调用?由于new运算符分配内存,因此请检查是否已分配内存,然后调用构造函数.但是,仅对于基元非数组堆分配,使用malloc比使用new更好吗?

But for non-array allocation, I think there wouldn't be any constructor call for int? Since, new operator allocates memory, checks if it's allocated and then calls the constructor. But just for primitives non-array heap allocation, is it better to use malloc than new?

请告知.

推荐答案

切勿在C ++中使用malloc.除非要实现低级内存管理原语,否则切勿使用new.

Never use malloc in C++. Never use new unless you are implementing a low-level memory management primitive.

建议是:

  • 问问自己:我需要动态内存分配吗?" .很多时候您可能不需要它-相对于指针,更喜欢 values 并尝试使用堆栈.

  • Ask yourself: "do I need dynamic memory allocation?". A lot of times you might not need it - prefer values to pointers and try to use the stack.

如果确实需要动态内存分配,请问自己谁将拥有分配的内存/对象?" .

If you do need dynamic memory allocation, ask yourself "who will own the allocated memory/object?".

  • 如果您只需要一个所有者(很有可能),则应 使用 std::unique_ptr .这是零成本的抽象 new/delete. (可以指定其他分配器 .)

  • If you only need a single owner (which is very likely), you should use std::unique_ptr. It is a zero cost abstraction over new/delete. (A different deallocator can be specified.)

如果需要共享所有权,则应使用 std::shared_ptr .这不是不是零成本抽象,因为它使用原子操作和额外的控制块"来跟踪所有所有者.

If you need shared ownership, you should use std::shared_ptr. This is not a zero cost abstraction, as it uses atomic operations and an extra "control block" to keep track of all the owners.

如果要特别处理数组,则标准库提供了两个功能强大且安全的抽象,不需要任何手动内存管理:

If you are dealing with arrays in particular, the Standard Library provides two powerful and safe abstractions that do not require any manual memory management:

  • std::array<T, N>: a fixed array of N elements of type T.

std::vector<T> :可调整大小的元素类型数组T.

std::arraystd::vector应该可以满足您99%的阵列需求".

std::array and std::vector should cover 99% of your "array needs".

另一件重要的事情:标准库提供了 std::make_unique std::make_shared ,它应该始终用于创建智能指针实例.有几个很好的理由:

One more important thing: the Standard Library provides the std::make_unique and std::make_shared which should always be used to create smart pointer instances. There are a few good reasons:

  • 缩略词-无需重复T (例如std::unique_ptr<T>{new T}),无需使用new.

更安全的异常.它们可以防止由于函数调用中缺少明确定义的求值顺序而导致的潜在内存泄漏.例如.

More exception safe. They prevent a potential memory leak caused by the lack of a well-defined order of evaluation in function calls. E.g.

f(std::shared_ptr<int>(new int(42)), g())

可以按以下顺序进行评估:

Could be evaluated in this order:

  1. new int(42)
  2. g()
  3. ...
  1. new int(42)
  2. g()
  3. ...

如果g()抛出,则int被泄漏.

效率更高(就运行速度而言).这仅适用于std::make_shared-使用它而不是std::shared_ptr直接允许实现为对象和控制块执行单个分配.

More efficient (in terms of run-time speed). This only applies to std::make_shared - using it instead of std::shared_ptr directly allows the implementation to perform a single allocation both for the object and for the control block.

您可以在此问题中找到更多信息..

You can find more information in this question.

这篇关于Malloc vs原始的新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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