- New (C++)
In the
C++ programming language , new is an operator that allows dynamicmemory allocation on the heap.new
attempts to allocate enough memory on the heap for the new data and, if successful, returns the address to the newly allocated memory.yntax
The syntax for
new
is::wherep_var
is a previously declared pointer of typetypename
.typename
can be any basic data type or user-defined object (enum
,class
, andstruct
included). Iftypename
is of class type, thedefault constructor is called to construct the object.To initialize a new variable created via
new
, use the following syntax::whereinitializer
is the initial value assigned to the new variable, or iftype
is of class type,initializer
is the arguments to a constructor.new
can also create anarray ::In this case,
size
specifies the length of one-dimensional array to create. The address of the first element is returned and stored intop_var
, so:gives the value of then
th element (counting from zero)Memory allocated with
new
must be deallocated withdelete
to avoid amemory leak . Arrays allocated withnew []
must be deallocated with[delete (C++)|delete []
.Initializers cannot be specified for arrays created with
new
. All elements of an array are initialized with the default constructor of the type. If the type does not have a default constructor, this is a compile-time error.Implementation
In
compilers conforming to the ISO C++ standard, if there is not enough memory for the allocation, the code throws an exception of typestd::bad_alloc
. All subsequent code is aborted until the error is handled in a try-catch block or the program exits abnormally. The program does not need to check the value of the pointer; if no exception was thrown, the allocation succeeded. The implemented operations are defined in the header<new>
. In most C++ implementations thenew
operator can also be overloaded to define specific behaviors.Releasing dynamically allocated memory
Any memory dynamically allocated with
new
must be released with adelete
command. There are two variants: one for arrays and one for single objects.Note that the compiler will generate neither a warning nor an error for using the wrong
delete
; it cannot know in general whether a pointer is to a single element or an array of elements. Furthermore, using the inappropriate deallocator can result in undefined behavior.Reallocating memory allocated by
new []
In contrast to C's
realloc
, it is not directly possible to reallocate memory allocated bynew []
; in order to extend or reduce the size of a block, one must allocate a new block of adequate size, copy over the old memory, and delete the old block.ee also
*
malloc
*Memory pool
*Pointers
*Smart pointers
*Exception handling References
* [http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05cplr199.htm IBM Documentation describing C++'s operator new]
* [http://msdn2.microsoft.com/en-us/library/kewsb8ba(VS.71).aspx Microsoft Visual Studio operator new documentation]
Wikimedia Foundation. 2010.