除了分配和ctor调用之外,C ++新操作符是什么? [英] What does the C++ new operator do other than allocation and a ctor call?

查看:192
本文介绍了除了分配和ctor调用之外,C ++新操作符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new 操作符除了分配内存和调用构造函数之外,还有什么其他事情?

What are all the other things the new operator does other than allocating memory and calling a constructor?

推荐答案

C ++标准可以说明来自< new> header:

The C++ standard has this to say about the single object form (the form usually used) of the new operator from the <new> header:


必需的行为:

Required behavior:

返回一个非空指针,存储(3.7.3),否则抛出一个
bad_alloc异常。

Return a nonnull pointer to suitably aligned storage (3.7.3), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function.

默认行为:

- 执行循环:在循环中,函数首先尝试分配所请求的存储。
尝试涉及调用标准C库函数malloc未指定。

— Executes a loop: Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to the Standard C library function malloc is unspecified.

- 如果尝试成功,返回一个指向已分配存储的指针。如果
set_new_handler()的最后一个参数是空指针,则抛出bad_alloc。

— Returns a pointer to the allocated storage if the attempt is successful. Otherwise, if the last argument to set_new_handler() was a null pointer, throw bad_alloc.

否则,函数调用当前的new_handler(18.4.2.2 )。如果被调用的函数返回,循环
重复。

— Otherwise, the function calls the current new_handler (18.4.2.2). If the called function returns, the loop repeats.

- 当尝试分配所请求的存储成功或者调用
new_handler函数不返回。

— The loop terminates when an attempt to allocate the requested storage is successful or when a called new_handler function does not return.

标准有很多关于新操作符和动态内存分配的其他东西可怕的很多说),但我认为默认行为列表总结了新操作的基础知识很好。

The standard has a lot of other stuff to say about the new operator and dynamic memory allocation (an awful lot to say), but I think the "Default behavior" list sums up the basics of the new operator pretty well.

这篇关于除了分配和ctor调用之外,C ++新操作符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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