Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/new"

From cppreference.com
< cpp‎ | memory
(fix type)
(various updates. Perhaps this could be merged into cpp/memory? It's a major topic.)
Line 1: Line 1:
 
{{title|Low level memory management}}
 
{{title|Low level memory management}}
 
{{cpp/memory/new/sidebar}}
 
{{cpp/memory/new/sidebar}}
===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 {{tt|<new>}}.  
  
===Deallocation===
 
 
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'').
 
 
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 begin}}
{{dcl list h2 | Classes}}
+
{{dcl list header |new}}
 +
{{dcl list h2 | Functions}}
 +
{{dcl list fun | cpp/memory/new/operator_new | title=operator new<br>operator new[] | allocation functions }}
 +
{{dcl list fun | cpp/memory/new/operator_delete | title=operator delete<br>operator delete[] | deallocation functions }}
 +
{{dcl list fun | cpp/memory/new/get_new_handler | obtains the current new handler | notes={{mark c++11}}}}
 +
{{dcl list fun | cpp/memory/new/set_new_handler | registers a different new handler}}
 +
{{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_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/bad_array_new_length | exception thrown on allocation of array with invalid 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 class | cpp/memory/new/nothrow_t | tag type used to select an non-throwing ''allocation function'' }}
 
+
{{dcl list h2 | Types }}
{{dcl list h2 | Types}}
+
{{dcl list typedef | cpp/memory/new/new_handler | function pointer type of the new handler }}
{{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 h2 | Objects}}
 
{{dcl list const | cpp/memory/new/nothrow | an object of type {{tt|nothrow_t}} used to select an non-throwing ''allocation function'' }}
 
{{dcl list const | cpp/memory/new/nothrow | an object of type {{tt|nothrow_t}} used to select an non-throwing ''allocation function'' }}
 
{{dcl list end}}
 
{{dcl list end}}

Revision as of 20:56, 27 October 2011

Template:cpp/memory/new/sidebar

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)
deallocation functions
(function)
obtains the current new handler
(function)
registers a different new handler
(function)
Classes
exception thrown when memory allocation fails
(class)
exception thrown on allocation of array with invalid length
(class)
tag type used to select an non-throwing allocation function
(class)
Types
function pointer type of the new handler
(typedef)
Objects
an object of type nothrow_t used to select an non-throwing allocation function
(constant)