39 The NEW Keyword in C++
When you write C++ code, you need to be concerned with memory, performance, and optimization issues, otherwise, it might be better to use another language.
The size of memory allocated is determined by the type you write, in bytes.
For example, new int
will request the allocation of 4 bytes of memory.
Now, we need to find a contiguous block of memory that contains 4 bytes. Although it is easy to find and can be allocated quickly, it still needs to find a 4-byte memory address in a row of memory. Once found, it will return a pointer to that memory address, allowing you to start using your data, whether for storage, access, reading, or writing.
When allocating memory, it doesn't scan memory continuously like a laser; instead, there is something called a free list that maintains addresses with free bytes.
In addition to creating an integer as usual, we can also choose dynamic memory allocation by using the new keyword on the heap.
This is the basic usage of the new keyword. Here, it not only allocates space but also calls the constructor.
To see the definition of new
:
_Size
is the size of memory it allocates, and it returns a void pointer.
You can review 16 POINTERS in C++.
Typically, calling the new keyword invokes the underlying C function malloc, which is used to allocate memory.
The actual function of malloc()
is to take a size
, which is the number of bytes we need, and then return a void pointer
.
delete
is also known as a destructor, see 26 Destructors in C++.
delete
is also an operator, which is just a regular function that calls the C function free()
. free can release the memory requested by malloc.
This is important because when we use the new keyword, the memory is not released, not marked as free, and it will not be returned to the free list. Therefore, when we call new again, this memory cannot be allocated again until we call delete
. We must manually release it.
Notes
-
If we use
new[]
, we should also usedelete[]
, because that's how the operator is defined. -
new supports a usage called placement new, which determines where its memory comes from.
We'll discuss the details later, but here we just show its syntax.
This constructs the Entity
object on the already allocated memory address b
, instead of using the default memory allocator. This allows creating an object at a specified memory location. This line of code constructs an Entity
object at the memory location pointed to by the b
pointer and returns a pointer to that object, assigning it to the e
pointer.