The C++14 standard (and later), brings a lot of useful smart pointers and improvements on them. They help us to avoid the mistake of incorrectly freeing the memory addressed by pointers. In modern C++, we have the new
and delete
operators that are used to allocate and free objects from memory, and in this post, we explain how to use new and delete operators.
Table of Contents
Can we use new and delete elisions in modern C++ or is it obsolete?
Allocating memory and freeing it safely is hard if you are programming a very big application. In Modern C++, there are smart pointers that help avoid the mistake of incorrectly freeing the memory addressed by pointers. Smart pointers make it easy to define pointers, they came with C++11. The most used types of C++ smart pointer are unique_ptr
, auto_ptr
, shared_ptr
, and weak_ptr
.
Smart pointers are preferable to raw pointers in many different scenarios, but there is still a lot of need to use for the new
and delete
methods in C++14 and above. When we develop code that requires in-place construction, we need to use new
and, possibly, delete
operations. They are useful, in a memory pool, as an allocator, as a tagged variant, as a buffer, or as a binary message to a buffer.
Sometimes we can use new and delete for some containers if we want to use raw pointers for storage. Modern C++ has a lot of modern choices for faster and safer memory operations. Generally, developers choose to use unique_ptr
/make_unique
and make_shared
rather than raw calls to new
and delete
. Even though we have a lot of standard smart pointers and abilities, we still need new
and/or delete
operators.
How can we use new and delete elisions in C++?
What is the new operator in C++?
The new
operator in C++, denotes a request for memory allocation on the free memory. If there is sufficient memory, a new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
Here, the data_type could be any built-in data type, i.e. basic C / C++ types, array, or any class types i.e. class, structure, union.
Now, let’s see how we can use new operator. We can simply use auto and new to create a new pointer and space for its buffer as below,
or in old style still you can use its type in definition instead of auto as below,
one of the great feature of pointers is we don’t need to allocate them at the beginning, we can set them NULL, and we can allocate them in some steps as below,
What is the delete operator in C++?
The delete operator in C++ is used to free the dynamically allocated array pointed by pointer variable.
Here is the syntax for the delete operator,
we can use delete[] for the pointer_arrays, here is the syntax for them,
here is how we can use delete to delete a pointer,
and this is how we can delete pointer arrays,
When we use delete or free or Free() in C++?
Please remember:
- Use delete operator ( or ->Free() method of objects) for the objects created by new operator (C++)
- Use free function for the variables created by malloc or other memory allocation methods (C/C++)
Here are more details and comparison table about using new-delete operators in C++ and malloc-free functions in C/C++.
How can we use new and Free() in C++?
In C++ Builder, generally, class types have a Free()
method to delete all objects. Most of classes in VCL/FMX libraries, components and other 3rd party components have this Free()
method. For example, we can create a bitmap (TBitmap) with the new
operator as below, then we can free it to be deleted in memory when we are done. Here is an example.
Note that, most of professional classes and their objects have Free()
method to be deleted safely in memory. In C++ Builder, most of components and classes have Free() method to be deleted. This method is not the same as free()
function that we use for malloc()
operations.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here