Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/new"

From cppreference.com
< cpp‎ | memory
m (Text replace - "{{mark c++0x}}" to "{{mark c++11}}")
m (fmt)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{title|Low level memory management}}
 
{{title|Low level memory management}}
{{cpp/memory/new/sidebar}}
+
{{cpp/memory/new/navbar}}
===Allocation===
+
  
The [[cpp/language/new | new expression]] is a language builtin which is the only way to initialize objects initialize objects in dynamically obtained memory. The memory is acquired by an {{rlt|operator_new | operator new, new[]}} (''allocation functions'').
+
The [[cpp/language/new |new-expression]] is the only way to create an object or an array of objects with dynamic storage duration, that is, with lifetime not restricted to the scope in which it is created. A new-expression obtains storage by calling an allocation function. A [[cpp/language/delete |delete-expression]] destroys a most derived object or an array created by a new-expression and calls the deallocation function. The default allocation and deallocation functions, along with related functions, types, and objects, are declared in the header {{header|new}}.  
  
===Deallocation===
+
{{dsc begin}}
 +
{{dsc header|new}}
 +
{{dsc h2|Functions}}
 +
{{dsc inc|cpp/memory/new/dsc operator_new}}
 +
{{dsc inc|cpp/memory/new/dsc operator_delete}}
 +
{{dsc inc|cpp/memory/new/dsc get_new_handler}}
 +
{{dsc inc|cpp/memory/new/dsc set_new_handler}}
 +
{{dsc h2|Classes}}
 +
{{dsc inc|cpp/memory/new/dsc bad_alloc}}
 +
{{dsc inc|cpp/memory/new/dsc bad_array_new_length}}
 +
{{dsc inc|cpp/memory/new/dsc align_val_t}}
 +
{{dsc h2|Types}}
 +
{{dsc inc|cpp/memory/new/dsc new_handler}}
 +
{{dsc h2|Objects}}
 +
{{dsc inc|cpp/memory/new/dsc nothrow}}
 +
{{dsc inc|cpp/memory/new/dsc destroying_delete}}
 +
{{dsc h2|Object access}}
 +
{{dsc inc|cpp/utility/dsc launder}}
 +
{{dsc end}}
  
The [[cpp/language/delete | delete expression]] is used to destruct objects previously initialized by the ''new'' expression and deallocate memory after that. For deallocation the ''delete'' expression uses {{rlt|operator_delete | operator delete, delete[] }} (''deallocation functions'').
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
+
Note, that it is possible to explicitly call the destructor of an object and deallocation function separately, if required.
+
 
+
===Miscellaneous===
+
 
+
The {{tt|<new>}} header contains declarations of default allocation and deallocation functions (note that these functions are implicitly defined in each translation unit). Also several utility functions, types and objects are declared
+
 
+
{{todo}}
+
{{dcl list begin}}
+
{{dcl list h2 | Classes}}
+
{{dcl list class | cpp/memory/new/bad_alloc | exception thrown when memory allocation fails }}
+
{{dcl list class | cpp/memory/new/bad_array_new_length | exception thrown on allocation of array with greater than <br> implementation supported, or negative length | notes={{mark c++11}} }}
+
{{dcl list class | cpp/memory/new/nothrow_t | tag type used to select an non-throwing ''allocation function'' }}
+
 
+
{{dcl list h2 | Types}}
+
{{dcl list class | cpp/memory/new/new_handler | function pointer type, used in {{rlf|set_new_handler}} | notes={{mark c++11}} }}
+
 
+
{{dcl list h2 | Functions}}
+
{{dcl list fun | cpp/memory/new/set_new_handler | sets a function which is called when an ''allocation function'' <br> fails to obtain more memory to the free memory pool | notes={{mark c++11}}}}
+
 
+
{{dcl list h2 | Objects}}
+
{{dcl list fun | cpp/memory/new/nothrow | an object of type {{tt|nothrow_t}} used to select an non-throwing ''allocation function'' }}
+
{{dcl list end}}
+

Latest revision as of 07:56, 16 December 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 

The new-expression is the only way to create an object or an array of objects with dynamic storage duration, that is, with lifetime not restricted to the scope in which it is created. A new-expression obtains storage by calling an allocation function. A delete-expression destroys a most derived object or an array created by a new-expression and calls the deallocation function. The default allocation and deallocation functions, along with related functions, types, and objects, are declared in the header <new>.

Defined in header <new>

Contents

Functions
allocation functions
(function) [edit]
deallocation functions
(function) [edit]
obtains the current new handler
(function) [edit]
registers a new handler
(function) [edit]
Classes
exception thrown when memory allocation fails
(class) [edit]
exception thrown on allocation of array with invalid length
(class) [edit]
type used to pass alignment to alignment-aware allocation and deallocation functions
(enum) [edit]
Types
function pointer type of the new handler
(typedef) [edit]
Objects
a tag used to select a non-throwing allocation function
(tag)[edit]
a tag used to select destroying-delete overloads of operator delete
(tag)[edit]
Object access
(C++17)
pointer optimization barrier
(function template) [edit]