- The
new
operator dynamically allocates memory for an object and calls
its constructor.
- It returns a
void
pointer to the allocated memory.
new
operator:
- Memory allocation: Calls
::operator new(size)
to allocate memory for the
object.
- Object initialization: Calls the constructor to initialize the object.
- Allocator usage:
- Calls the allocator’s
allocate
function to allocate memory.
- Calls the allocator’s
construct
function to initialize the object.
new[]
operator:
- Memory allocation: Calls :
:operator new[](size)
to allocate memory for an
array of elements.
- Element initialization: Calls the constructor for each element in the
array.
- Allocator usage:
- Calls the allocator’s allocate function to
allocate
memory for the
array.
- Calls the allocator’s
construct
function to initialize each element.
Type* ptr = new Type; // For a single object.
Type* arr_ptr = new Type[size]; // For an array objects.
- The placement
new
operator allows you to construct an object at a
specified memory address, which is useful for optimizing memory usage
or working with pre-allocated memory.
- It also calls the constructor of the object and returns a
void
pointer to the allocated memory.
Type* ptr = new( address ) Type; // For a single object.
Type* arr_ptr = new( address ) Type[size]; // For an array objects.
// Allocate memory on the stack.
// Its lifetime is managed by its scope.
// `delete` should not be used to `delete` it.
char buffer[sizeof( ClassName )];
Type* arr_ptr = new( buffer ) ClassName();
- The
delete
operator deallocates memory that was previously allocated
with new
, calling the destructor of the object before releasing
the memory.
- After calling
delete
, it's a good practice to set the pointer to nullptr
to avoid dangling pointers.
delete
operator:
- Object destruction:
- Calls the destructor to destroy the object.
- Allocator usage:
- Calls the allocator’s
destroy
function to destroy the object.
- Calls the allocator’s
deallocate
function to deallocate memory.
- Memory deallocation:
- Calls
::operator delete(ptr)
to deallocate memory for the object.
delete[]
operator:
- Object destruction:
- Calls the destructor for each element in the array.
- Allocator usage:
- Calls the allocator’s
destroy
function to destroy each object.
- Calls the allocator’s
deallocate
function to deallocate memory.
- Memory deallocation:
- Calls
::operator delete[](ptr)
to deallocate memory for the array.
delete ptr; // For a single object created with `new`.
ptr = nullptr; // Good practice to avoid dangling pointers.
delete[] arr_ptr; // For an array object created with `new[]`.
arr_ptr = nullptr; // Good practice to avoid dangling pointers.
::operator new
is a global operator function that allocates raw
memory without initializing it.
- It's similar to
malloc
, but it can be overridden to customize memory
allocation for user-defined types.
- It does not call constructors and only allocates memory.
::operator delete
should be used to free memory allocated with
::operator new
.
// Declaration syntax.
void* ptr = ::operator new( size_t size );
// Its usage syntax.
Type* ptr = static_cast< Type* >( ::operator new( num * sizeof( Type ) ) );
::operator delete
is a global operator function that deallocates
memory previously allocated with ::operator new
.
- It's similar to
free
, but it can be overridden to customize memory
deallocation for user-defined types.
::operator delete
does not call destructors and only frees the
memory.
- After calling
::operator delete
, it's a good practice to set the pointer to
nullptr
to avoid dangling pointers.
// Declaration syntax.
void operator delete( void* ptr ) noexcept;
// Its usage syntax.
::operator delete( ptr );
ptr = nullptr; // Good practice to avoid dangling pointers.
malloc
calloc
realloc
free
- Pointers
- When a temporary object is created using
new
, malloc
, or calloc
and is
passed to a function or object, the function or object is responsible for
destroying the temporary object.
- In fact, when
new
, malloc
, or calloc
is called within a pair of
parentheses, the owner of the parentheses is the caller of new
, malloc
,
or calloc
.
- The caller is responsible for destroying the dynamic memory.