Skip to content

Commit c4b7b25

Browse files
authored
Merge pull request #2979 from corob-msft/docs/corob/cpp-docs.ja-jp235
Address cpp docs.ja jp issue 235
2 parents 6d57ccd + 23ffc1e commit c4b7b25

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

docs/cpp/new-and-delete-operators.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
---
2-
title: "new and delete Operators"
3-
ms.date: "11/19/2019"
2+
title: "new and delete operators"
3+
description: "The C++ language new and delete operators allow control over allocation."
4+
ms.date: 07/07/2020
45
helpviewer_keywords: ["new keyword [C++]", "delete keyword [C++]"]
5-
ms.assetid: fa721b9e-0374-4f04-bb87-032ea775bcc8
66
---
7-
# new and delete operators
7+
# `new` and `delete` operators
88

9-
C++ supports dynamic allocation and deallocation of objects using the [new](new-operator-cpp.md) and [delete](delete-operator-cpp.md) operators. These operators allocate memory for objects from a pool called the free store. The **new** operator calls the special function [operator new](new-operator-cpp.md), and the **delete** operator calls the special function [operator delete](delete-operator-cpp.md).
9+
C++ supports dynamic allocation and deallocation of objects using the [`new`](new-operator-cpp.md) and [`delete`](delete-operator-cpp.md) operators. These operators allocate memory for objects from a pool called the free store. The **`new`** operator calls the special function [`operator new`](new-operator-cpp.md), and the **`delete`** operator calls the special function [`operator delete`](delete-operator-cpp.md).
1010

11-
The **new** function in the C++ Standard Library supports the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails. If you still want the non-throwing version of **new**, link your program with nothrownew.obj. However, when you link with nothrownew.obj, the default **operator new** in the C++ Standard Library no longer functions.
11+
The **`new`** function in the C++ Standard Library supports the behavior specified in the C++ standard, which is to throw a `std::bad_alloc` exception if the memory allocation fails. If you still want the non-throwing version of **`new`**, link your program with *`nothrownew.obj`*. However, when you link with *`nothrownew.obj`*, the default **`operator new`** in the C++ Standard Library no longer functions.
1212

13-
For a list of the library files that comprise the C Runtime Library and the C++ Standard Library, see [CRT Library Features](../c-runtime-library/crt-library-features.md).
13+
For a list of the library files in the C Runtime Library and the C++ Standard Library, see [CRT Library Features](../c-runtime-library/crt-library-features.md).
1414

15-
## <a id="new_operator"> </a> The new operator
15+
## <a id="new_operator"> </a> The `new` operator
1616

17-
When a statement such as the following is encountered in a program, it translates into a call to the function **operator new**:
17+
The compiler translates a statement such as this one into a call to the function **`operator new`**:
1818

1919
```cpp
2020
char *pch = new char[BUFFER_SIZE];
2121
```
2222

23-
If the request is for zero bytes of storage, **operator new** returns a pointer to a distinct object (that is, repeated calls to **operator new** return different pointers). If there is insufficient memory for the allocation request, **operator new** throws a `std::bad_alloc` exception, or returns **nullptr** if you have linked in non-throwing **operator new** support.
23+
If the request is for zero bytes of storage, **`operator new`** returns a pointer to a distinct object. That is, repeated calls to **`operator new`** return different pointers. If there's insufficient memory for the allocation request, **`operator new`** throws a `std::bad_alloc` exception. Or, it returns **`nullptr`** if you've linked in non-throwing **`operator new`** support.
2424

25-
You can write a routine that attempts to free memory and retry the allocation; see [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) for more information. For more details on the recovery scheme, see the Handling insufficient memory section of this topic.
25+
You can write a routine that attempts to free memory and retry the allocation. For more information, see [`_set_new_handler`](../c-runtime-library/reference/set-new-handler.md). For details on the recovery scheme, see the [Handling insufficient memory](#handling-insufficient-memory) section.
2626

27-
The two scopes for **operator new** functions are described in the following table.
27+
The two scopes for **`operator new`** functions are described in the following table.
2828

29-
### Scope for operator new functions
29+
### Scope for `operator new` functions
3030

31-
|Operator|Scope|
32-
|--------------|-----------|
33-
|**::operator new**|Global|
34-
|*class-name* **::operator new**|Class|
31+
| Operator | Scope |
32+
|--|--|
33+
| **`::operator new`** | Global |
34+
| *class-name* **`::operator new`** | Class |
3535

36-
The first argument to **operator new** must be of type `size_t` (a type defined in \<stddef.h>), and the return type is always **void** <strong>\*</strong>.
36+
The first argument to **`operator new`** must be of type `size_t`, defined in \<stddef.h>, and the return type is always **`void*`**.
3737

38-
The global **operator new** function is called when the **new** operator is used to allocate objects of built-in types, objects of class type that do not contain user-defined **operator new** functions, and arrays of any type. When the **new** operator is used to allocate objects of a class type where an **operator new** is defined, that class's **operator new** is called.
38+
The global **`operator new`** function is called when the **`new`** operator is used to allocate objects of built-in types, objects of class type that don't contain user-defined **`operator new`** functions, and arrays of any type. When the **`new`** operator is used to allocate objects of a class type where an **`operator new`** is defined, that class's **`operator new`** is called.
3939

40-
An **operator new** function defined for a class is a static member function (which cannot, therefore, be virtual) that hides the global **operator new** function for objects of that class type. Consider the case where **new** is used to allocate and set memory to a given value:
40+
An **`operator new`** function defined for a class is a static member function (which can't be virtual) that hides the global **`operator new`** function for objects of that class type. Consider the case where **`new`** is used to allocate and set memory to a given value:
4141

4242
```cpp
4343
#include <malloc.h>
@@ -66,13 +66,13 @@ int main()
6666
}
6767
```
6868

69-
The argument supplied in parentheses to **new** is passed to `Blanks::operator new` as the `chInit` argument. However, the global **operator new** function is hidden, causing code such as the following to generate an error:
69+
The argument supplied in parentheses to **`new`** is passed to `Blanks::operator new` as the `chInit` argument. However, the global **`operator new`** function is hidden, causing code such as the following to generate an error:
7070

7171
```cpp
7272
Blanks *SomeBlanks = new Blanks;
7373
```
7474

75-
The compiler supports member array **new** and **delete** operators in a class declaration. For example:
75+
The compiler supports member array **`new`** and **`delete`** operators in a class declaration. For example:
7676

7777
```cpp
7878
class MyClass
@@ -111,28 +111,28 @@ int main() {
111111
}
112112
```
113113
114-
There is another way to handle failed memory allocation requests. Write a custom recovery routine to handle such a failure, then register your function by calling the [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) run-time function.
114+
There's another way to handle failed memory allocation requests. Write a custom recovery routine to handle such a failure, then register your function by calling the [`_set_new_handler`](../c-runtime-library/reference/set-new-handler.md) run-time function.
115115
116-
## <a id="delete_operator"> </a> The delete operator
116+
## <a id="delete_operator"> </a> The `delete` operator
117117
118-
Memory that is dynamically allocated using the **new** operator can be freed using the **delete** operator. The delete operator calls the **operator delete** function, which frees memory back to the available pool. Using the **delete** operator also causes the class destructor (if there is one) to be called.
118+
Memory that is dynamically allocated using the **`new`** operator can be freed using the **`delete`** operator. The delete operator calls the **`operator delete`** function, which frees memory back to the available pool. Using the **`delete`** operator also causes the class destructor (if one exists) to be called.
119119
120-
There are global and class-scoped **operator delete** functions. Only one **operator delete** function can be defined for a given class; if defined, it hides the global **operator delete** function. The global **operator delete** function is always called for arrays of any type.
120+
There are global and class-scoped **`operator delete`** functions. Only one **`operator delete`** function can be defined for a given class; if defined, it hides the global **`operator delete`** function. The global **`operator delete`** function is always called for arrays of any type.
121121
122-
The global **operator delete** function. Two forms exist for the global **operator delete** and class-member **operator delete** functions:
122+
The global **`operator delete`** function. Two forms exist for the global **`operator delete`** and class-member **`operator delete`** functions:
123123
124124
```cpp
125125
void operator delete( void * );
126126
void operator delete( void *, size_t );
127127
```
128128

129-
Only one of the preceding two forms can be present for a given class. The first form takes a single argument of type `void *`, which contains a pointer to the object to deallocate. The second formsized deallocationtakes two arguments, the first of which is a pointer to the memory block to deallocate and the second of which is the number of bytes to deallocate. The return type of both forms is **void** (**operator delete** cannot return a value).
129+
Only one of the preceding two forms can be present for a given class. The first form takes a single argument of type **`void *`**, which contains a pointer to the object to deallocate. The second form, sized deallocation, takes two arguments: the first is a pointer to the memory block to deallocate, and the second is the number of bytes to deallocate. The return type of both forms is **`void`** (**`operator delete`** can't return a value).
130130

131-
The intent of the second form is to speed up searching for the correct size category of the object to be deleted, which is often not stored near the allocation itself and likely uncached. The second form is useful when an **operator delete** function from a base class is used to delete an object of a derived class.
131+
The intent of the second form is to speed up searching for the correct size category of the object to delete. This information often isn't stored near the allocation itself, and is likely uncached. The second form is useful when an **`operator delete`** function from a base class is used to delete an object of a derived class.
132132

133-
The **operator delete** function is static; therefore, it cannot be virtual. The **operator delete** function obeys access control, as described in [Member-Access Control](member-access-control-cpp.md).
133+
The **`operator delete`** function is static, so it can't be virtual. The **`operator delete`** function obeys access control, as described in [Member-Access Control](member-access-control-cpp.md).
134134

135-
The following example shows user-defined **operator new** and **operator delete** functions designed to log allocations and deallocations of memory:
135+
The following example shows user-defined **`operator new`** and **`operator delete`** functions designed to log allocations and deallocations of memory:
136136

137137
```cpp
138138
#include <iostream>
@@ -180,9 +180,9 @@ int main( int argc, char *argv[] ) {
180180
}
181181
```
182182
183-
The preceding code can be used to detect "memory leakage"that is, memory that is allocated on the free store but never freed. To perform this detection, the global **new** and **delete** operators are redefined to count allocation and deallocation of memory.
183+
The preceding code can be used to detect "memory leakage", that is, memory that's allocated on the free store but never freed. To detect leaks, the global **`new`** and **`delete`** operators are redefined to count allocation and deallocation of memory.
184184
185-
The compiler supports member array **new** and **delete** operators in a class declaration. For example:
185+
The compiler supports member array **`new`** and **`delete`** operators in a class declaration. For example:
186186
187187
```cpp
188188
// spec1_the_operator_delete_function2.cpp

0 commit comments

Comments
 (0)