new expression
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
初始化对象中动态获取的内存.
原文:
Initializes objects in dynamically obtained memory.
使用数据需要动态分配,就是不知道它的大小之前,编译.
原文:
Used where data need to be allocated dynamically, that is, without knowing its size prior the compilation.
目录 |
[编辑] 语法
:: (可选)
|
new
|
type | [ array_n] (可选)
|
( init_params) (可选)
|
(1) | ||||
:: (可选)
|
new
|
( type )
|
[ array_n] (可选)
|
( init_params) (可选)
|
(2) | ||||
:: (可选)
|
new
|
( placement_params)
|
type | [ array_n] (可选)
|
( init_params) (可选)
|
(3) | |||
:: (可选)
|
new
|
( placement_params)
|
( type )
|
[ array_n] (可选)
|
( init_params) (可选)
|
(4) | |||
所有版本都返回一个对象类型型*.
原文:
All versions return an object of type type *.
[编辑] 解释
new
表达式分配一个内存区域,无论是单个对象或一个对象数组进行初始化,并返回一个指针,指向第一个构造的对象。由于它不是,否则可能调用显式调用构造函数,表达的是动态构造一个对象,只有这样,才能.原文:
The
new
expression allocates a memory area, initializes either single object, or an array of objects there and returns a pointer to the first constructed object. Since it is not otherwise possible to call explicitly call a constructor, the expression is the only way to construct an object dynamically.前两个和最后两个版本,不同的只是语法的表达。的行为是一样的
原文:
The first two and last two versions of the expression differ only syntactically. The behavior is the same.
本章尚未完成 原因:Explanation about how types are specified is missing |
[编辑] 内存分配
最近的两个版本中的表达被称为安置新的和分配功能是用来传递额外的参数(placement_params).
原文:
The last two versions of the expression are called placement new and are used to pass additional parameters (placement_params) to the allocation function.
如果array_n是不存在的,内存被分配为一个单一的对象operator new分配函数调用。否则array_n对象的数组的内存分配调用operator new[]分配功能。请注意,以上
size_of(
type) *
array_n可能被分配,因为额外的信息编码,由编译器(如阵列的大小,因为这个信息是需要的,以便破坏在数组中的对象正确).原文:
If array_n is absent, memory is allocated for a single object by invoking operator new allocation function. Otherwise memory for an array of array_n objects is allocated by calling operator new[] allocation function. Note, that more than
size_of(
type ) *
array_n might be allocated because of additional information encoded by the compiler (such as the size of the array, since this information is needed in order to destruct the objects in the array properly).分配函数的名称首先在本地类“类型”的范围抬起头来,只有当查找失败,全局命名空间抬起头来。
::
是new
表达,只有全局命名空间抬起头来。看起来应该像下面这样的功能的原型,以分配功能可供选择: 原文:
The allocation function's name is firstly looked up in the local class type scope and only if the lookup fails, the global namespace is looked up. If
::
is present in the new
expression, only the global namespace is looked up. The prototype of the function should look like the following in order to the allocation function to be selected: void* operator new (size_t count); |
for versions 1, 2, array_n is not present | |
void* operator new[](size_t count); |
for versions 1, 2, array_n is present | |
void* operator new (size_t count/*, placement_params...*/); |
for versions 3, 4 (placement new), array_n is not present | |
void* operator new[](size_t count/*, placement_params...*/); |
for versions 3, 4 (placement new), array_n is present | |
count
是要分配的字节数,placement_params
是给定的参数的位置“新”的表达.原文:
count
is number of bytes to allocate, placement_params
are the parameters given to the placement new expression.[编辑] 默认的实现和重载
有几个默认的分配函数的隐式声明每个翻译单元。此外,隐式实现由编译器在默认情况下,除非程序显式实现。这些功能如下(有关详细信息,请参阅这一点)
原文:
Several default allocation functions are implicitly declared in each translation unit. Also, implicit implementations are provided by the compiler by default, unless the program has explicitly implemented them. These functions are as follows (see 这一点 for more information):
void* operator new (size_t); |
(1) | |
void* operator new[](size_t); |
(2) | |
void* operator new (size_t, std::nothrow_t); |
(3) | |
void* operator new[](size_t, std::nothrow_t); |
(4) | |
void* operator new (size_t, void* ptr); |
(5) | |
void* operator new[](size_t, void* ptr); |
(6) | |
分配请求的字节数或抛出故障std::bad_alloc上
3-4) 原文:
allocates requested number of bytes or throws std::bad_alloc on failure
分配请求数量的字节或返回NULL上的失败
5-6) 原文:
allocates requested number of bytes or returns NULL on failure
返回
ptr
。这允许用户提供的内存区域中构造一个对象。没有定义明确的程序必须实现这些功能.原文:
returns
ptr
. This allows to construct an object in user-supplied memory area. The program must not define explicit implementations for these functions.[编辑] 对象的初始化
array_n是不存在的,单一的对象初始化收购的内存区域,通过init_params作为参数传递给构造函数或调用默认的构造函数,如果它们不存在.
原文:
If array_n is absent, single object is initialized in the acquired memory area, passing init_params as parameters to the constructor or invoking default constructor if they are not present.
array_n是目前array_n对象,数组的初始化,通过init_params作为参数传递给每一个对象的构造函数或init_params调用默认的构造函数,如果不存在.
原文:
If array_n is present, an array of array_n objects is initialized, passing init_params as parameters to the constructor of each object or invoking default constructor if init_params are not present.