new expression
Da 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. |
Inicializa os objetos na memória dinamicamente obtido.
Original:
Initializes objects in dynamically obtained 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.
Usado onde os dados precisam ser alocados de forma dinâmica, isto é, sem saber o seu tamanho antes da compilação.
Original:
Used where data need to be allocated dynamically, that is, without knowing its size prior the compilation.
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.
Índice |
[editar] Sintaxe
:: (opcional)
|
new
|
type | [ array_n] (opcional)
|
( init_params) (opcional)
|
(1) | ||||
:: (opcional)
|
new
|
( type )
|
[ array_n] (opcional)
|
( init_params) (opcional)
|
(2) | ||||
:: (opcional)
|
new
|
( placement_params)
|
type | [ array_n] (opcional)
|
( init_params) (opcional)
|
(3) | |||
:: (opcional)
|
new
|
( placement_params)
|
( type )
|
[ array_n] (opcional)
|
( init_params) (opcional)
|
(4) | |||
Todas as versões retornar um objeto do tipo' 'tipo *.
Original:
All versions return an object of type type *.
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.
[editar] Explicação
A expressão
new
aloca uma área de memória, inicializa ou único objeto, ou um array de objetos lá e retorna um ponteiro para o primeiro objeto construído. Como não é possível de outra forma de chamar explicitamente chamar um construtor, a expressão é a única maneira de construir um objeto dinamicamente.Original:
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 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.
As duas primeiras e última duas versões da expressão só diferem sintaticamente. O comportamento é o mesmo.
Original:
The first two and last two versions of the expression differ only syntactically. The behavior is the same.
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.
Esta seção está incompleta Motivo: Explanation about how types are specified is missing |
[editar] Alocação de memória
A memória é alocada por um função de alocação, seja operator new ou operator new[].
Original:
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.
As últimas duas versões da expressão são chamados' 'nova colocação e são utilizados para transmitir os parâmetros adicionais (placement_params) para a função de distribuição.
Original:
The last two versions of the expression are called placement new and are used to pass additional parameters (placement_params) to the allocation function.
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.
Se array_n está ausente, a memória é alocada para um único objeto, invocando operator new função de alocação. Caso contrário memória para uma matriz de objetos array_n é alocado chamando operator new[] função de alocação. Note que, mais do que
size_of(
type ) *
array_n pode ser alocada por causa da informação adicional codificada pelo compilador (tais como o tamanho da matriz, uma vez que esta informação é necessária, a fim de destruir os objetos na matriz adequadamente).Original:
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).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.
O nome da função de alocação está em primeiro lugar procurado no local de classe' tipo de escopo e somente se a pesquisa falhar, o espaço global é procurado. Se
::
está presente na expressão new
, somente o namespace global está olhou para cima. O protótipo da função deve ser semelhante à seguinte para a função de alocação a ser selecionados: Original:
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: 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); |
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
é o número de bytes a serem alocados, placement_params
são os parâmetros determinados para a colocação expressão' novo.Original:
count
is number of bytes to allocate, placement_params
are the parameters given to the placement 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.
[editar] Implementações padrão e sobrecarga
Várias funções de alocação padrão são implicitamente declarados em cada unidade de tradução. Além disso, as implementações implícitas são fornecidos pelo compilador por padrão, a menos que o programa foi implementado explicitamente eles. Estas funções são as seguintes (ver este para mais informações):
Original:
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 este for more information):
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); |
(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) | |
aloca solicitado número de bytes ou joga std::bad_alloc em caso de falha
3-4) Original:
allocates requested number of bytes or throws std::bad_alloc on failure
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.
aloca solicitado número de bytes ou NULL retornos em caso de falha
5-6) Original:
allocates requested number of bytes or returns NULL on failure
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
retornos. Isto permite a construção de um objeto em fornecido pelo usuário área de memória. O programa não deve definir implementações explícitas para essas funções.Original:
returns
ptr
. This allows to construct an object in user-supplied memory area. The program must not define explicit implementations for these functions.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.
[editar] Inicialização do objeto
Se array_n está ausente, único objeto é inicializado na área de memória adquirida, passando init_params como parâmetros para o construtor ou invocando construtor padrão se eles não estiverem presentes.
Original:
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.
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.
Se array_n está presente, um array de objetos array_n é inicializado, passando init_params como parâmetros para o construtor de cada objeto ou invocar construtor padrão se init_params não estão presentes.
Original:
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.
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.