new (Command)

Written By: Ehsan Jahandarpour

In the C++ programming language, as well as in many C++-based languages, new is a language construct that dynamically allocates memory from free store and initialises the memory using the constructor. Except for a form called the “placement new”, new attempts to allocate enough memory in free store for the new data. If successful, it initialises the memory and returns the address to the newly allocated and initialised memory. However if new cannot allocate memory in free store it will throw an exception of type std::bad_alloc. This removes the need to explicitly check the result of an allocation. A call to delete, which calls the destructor and returns the memory allocated by new back to free store, must be made for every call to new to avoid a memory leak.