<div class="t-tr-text">运营商<div class="t-tr-dropdown"><div><div><div class="t-tr-dropdown-arrow-border"></div><div class="t-tr-dropdown-arrow"></div><div class="t-tr-dropdown-h">Original:</div><div class="t-tr-dropdown-orig">operator</div><div class="t-tr-dropdown-notes">The text has been machine-translated via [http://translate.google.com Google Translate].<br/> You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.</div></div></div></div></div> new<div class="t-tr-text">,操作员<div class="t-tr-dropdown"><div><div><div class="t-tr-dropdown-arrow-border"></div><div class="t-tr-dropdown-arrow"></div><div class="t-tr-dropdown-h">Original:</div><div class="t-tr-dropdown-orig">, operator</div><div class="t-tr-dropdown-notes">The text has been machine-translated via [http://translate.google.com Google Translate].<br/> You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.</div></div></div></div></div> new[]
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <new>
|
||
| void* operator new ( std::size_t count ); |
(1) | |
| void* operator new[]( std::size_t count ); |
(2) | |
| void* operator new ( std::size_t count, const std::nothrow_t& ); |
(3) | |
| void* operator new[]( std::size_t count, const std::nothrow_t& ); |
(4) | |
| void* operator new ( std::size_t, void* ptr ); |
(5) | |
| void* operator new[]( std::size_t, void* ptr ); |
(6) | |
分配请求的字节数。这些分配的功能被称为新的表达式分配的内存中,然后将新的对象初始化.
1-2) Original:
Allocates requested number of bytes. These allocation functions are called by 新的表达式 to allocate memory in which new object would then be initialized.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
从自由存储区分配
3-4) count字节。调用该函数返回的指针,失败和重复分配,直到新的处理函数没有返回,或成为一个空指针,此时抛出std::get_new_handlerstd::bad_alloc.Original:
Allocates
count bytes from free store. Calls the function pointer returned by std::get_new_handler on failure and repeats allocation attempts until new handler does not return or becomes a null pointer, at which time throws std::bad_alloc.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1-2相同,但返回一个空指针时,,1-2将抛出std::bad_alloc
5-6) Original:
Same as 1-2, but returns a null pointer when 1-2 would throw std::bad_alloc
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
什么也不做,返回
ptr。这些版本被称为新的表达在先前分配的存储空间,构造对象的.Original:
does nothing, returns
ptr. These versions are called by new-expression which construct objects in previously allocated storage.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 更换和超载
<new>头即使不包括在每个翻译单元的版本1-4)隐式声明。这些功能是“可替换”用户提供的非成员函数具有相同签名取代了隐式的版本。至多有一个更换,也可以设置为每个四个隐式分配函数。此外,程序可以定义这些函数或类成员的版本定义分配功能具有不同签名的(除非这是不允许的更换(5-6)的分配功能的版本)。增加的签名看起来应该像下面这样,count是要分配的字节数和“placement_params”提供的参数new表达Original:
The versions 1-4) are implicitly declared in each translation unit even if the
<new> header is not included. These functions are replaceable: a user-provided non-member function with the same signature replaces the implicit version. At most one replacement may be provided for each of the four implicit allocation functions. Also, program can define class member versions of these functions or define allocation functions with different signatures (except that it is not permitted to replace (5-6) versions of the allocation function). The added signature should look like the following, where count is number of bytes to allocate and placement_params are the parameters supplied to the new expression:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
| void* operator new (size_t count/*, placement_params*/); |
for the new version |
|
| void* operator new[](size_t count/*, placement_params*/); |
for the new[] version |
|
分配函数可以被替换/重载的方法有两种:
Original:
The allocation function can be replaced/overloaded in two ways:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- “”在全球范围内:为了调用它,重载的分配函数的签名必须是可见的,在分配的地方,除了隐式声明的默认配置功能。这种分配功能将被用于所有的分配在当前程序中相应的参数Original:in the global scope: in order to call it, the signature of the overloaded allocation functions must be visible at the place of allocation, except for implicitly declared default allocation functions. This allocation function will be used for all allocations with corresponding parameters in the current programThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 在局部范围内重载
operator new必须是静态的公有成员函数的类。这种分配功能将仅用于该特定类别的分配.Original:in the local scope: the overloadedoperator newmust be static public member function of the class. This allocation function will be used only for allocations of that particular class.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
在编译过程中,每个
new表达看起来相应的分配函数的名称首先在类范围后,在全球范围内。可以通过调用new为::new指示跳过第一步。请注意,每超载的规则类的范围内,任何分配中声明的函数隐藏了所有的全球配置功能。欲了解更多信息,请参阅NJ表达。请注意,这是不可能放置在一个命名空间分配函数.Original:
During compilation, each
new expression looks up for appropriate allocation function's name firstly in the class scope and after that in the global scope. It can be instructed to skip the first step by calling new as ::new. Note, that as per 超载的规则, any allocation functions declared in class scope hides all global allocation functions. For more information see NJ表达. Note, that it is not possible to place allocation function in a namespace.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 参数
| count | - | 要分配的字节数
Original: number of bytes to allocate The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| ptr | - | 指向的存储区域的指针,以初始化该对象
Original: pointer to a memory area to initialize the object at The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
1-4)分配的内存区域的指针
Original:
pointer to allocated memory area
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5-6) ptr
[编辑] 例外
1-2)内存分配失败,抛出std::bad_alloc
3-6)
Original:
throws std::bad_alloc on failure to allocate memory
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 另请参阅
| 释放函数 Original: deallocation functions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) | |
| (C++11) |
获得新的处理程序 Original: obtains the current new handler The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) |
| 注册一个新的处理程序 Original: registers a new handler The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) | |
| 获得未初始化的存储 Original: obtains uninitialized storage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) | |
| 分配内存 Original: allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数) | |