I am learning C ++ and I find that there are different ways to free dynamic memory ( delete
, delete[]
, free(...)
) and I do not know what cases to use them, can someone get me out of this confusion? Thanks beforehand
I am learning C ++ and I find that there are different ways to free dynamic memory ( delete
, delete[]
, free(...)
) and I do not know what cases to use them, can someone get me out of this confusion? Thanks beforehand
The C ++ standard categorizes memory management in hosting functions and un-hosted functions, as can be seen in section 3.7.4.1 (my translation):
3.7.4 Dynamic Storage Duration
Objects can be created dynamically during the execution of the program, using expressions-new , and destroyed using expressions-delete . C ++ implementations provide access and management to dynamic meomria through global hosting functions
operator new
andoperator new[]
and global unsubscribe functionsoperator delete
andoperator delete[]
.
These are the functions of hosted and de-hosted memory that C ++ offers, the variant with brackets ( []
) is used to host or de-store memory of formations 1 . The memory un-hosting feature std::free
belongs to C, not to C ++, it is possible to use it in C ++ code and it is the only way to free memory hosted by
std::malloc
.
Broadly speaking, all the memory hosted by C ++ functions ( new
and new[]
) must be de-hosted by C ++ functions ( delete
and delete[]
) while all the memory hosted by the C host function ( std::malloc
) must be un-hosted by the un-hosted function C ( std::free
).
If we analyze in more detail the hosted and un-hosted functions of C ++ and C:
C ++: new
/ new[]
with delete
/ delete[]
:
std::bad_alloc
new
/ new[]
has strong typing, the parameter that receives delete
/ delete[]
is also strongly typed.
new
/ delete
) is explicitly separated from the version for formations ( new[]
/ delete[]
). C: std::malloc
with std::freee
:
std::malloc
lacks type information, nor does it have type information the parameter that receives std::free
.
free
is a function inherited from C. In C there is no concept of classes and objects. It is a pure and hard procedural language. Under this context, to work with dynamic memory we find three basic functions:
malloc
: Function that reserves a block of memory and returns a pointer to the beginning of that block. realloc
: Function that allows to enlarge or reduce the previously reserved memory block. If you pass a null block it works just like malloc
. free
: Function that releases the previously reserved memory with malloc
. There is also the function calloc
, but I have not included it in this list because this function is rather composite. This function makes a memory reservation, like malloc
, and then initializes all the bytes of said reservation to a certain value.
In C ++ things change. C ++ is an object-oriented language and, for this reason, it has a series of utilities that facilitate working with objects. In the case of management with dynamic memory we have the following functions:
new
: This function is the big sister of calloc
. Apart from reserving a sufficiently large block of memory, it automatically invokes the constructor to initialize the memory with appropriate values.
delete
: This function is responsible for invoking the destructor of the object, then releasing the memory block.
These two functions only serve to create loose elements, one by one. For those cases where it is necessary to create arrays of elements we have at our disposal a version of new
and delete
specific. This version is characterized by brackets:
int* elementoSuelto = new int(10); // Un unico entero, valor 10
int* array = new int[5](10); // 5 enteros, todos valen 10
delete elementoSuelto;
delete[] array;
At this point it is important to note that it is important to know which version of delete
should be used at each moment. If we create an element with new
we will have to use delete
, but if it is created with new[]
then we should use delete[]
. If we mix the uses we can find erratic behavior, memory leaks and other problems when running the application.
Well, summarizing, these are the main differences between the functions that you comment:
free
: It is the simplest of all. This function is limited to marking as free the block of memory that is passed through pointer. delete
: Own C ++ utility. This function invokes the destructor of the object and, finally, releases the memory reserved by the same delete[]
: Similar to the previous utility, it is intended to erase object arrays. I was looking at information in This book in chapter 104.11 and I found this:
int* p1 = new int;
delete p1; // correct
// delete[] p1; // undefined
// free(p1); // undefined
int* p2 = new int[10];
delete[] p2; // correct
// delete p2; // undefined
// free(p2); // undefined
int* p3 = static_cast<int*>(malloc(sizeof(int)));
free(p3); // correct
// delete p3; // undefined
// delete[] p3; // undefined
delete
:
An object that was created with new
and is not an array can only free the memory with delete
delete[]
:
An object that is created with new __[..]
or with char *
can only free the memory with delete[]
since it not only has a memory location but has a memory chain
free(...)
:
An Object which set aside memory with malloc(sizeof(...))
can only free the memory with free(...)