The New Keyword in C++

Mayowa Obisesan
4 min readNov 13, 2023

--

Generated by Bing Image Creator — Powered by Dall-e 3

The main function of the new keyword in C++ is to allocate memory on the heap specifically. The new keyword finds a block of memory that is big enough to accommodate our needs and returns a pointer to that block of memory.

Here are some of the properties of the new keyword that are significant to this article:

  1. The new Keyword not only allocates memory, it also calls the constructor.
  2. The new keyword in C++ that takes in size as an argument and returns a pointer to a block of memory.
  3. The new keyword is an operator in C++. Which means you can overload the operator new and change its behaviour. I’ll use the term new operator in place of new keyword for the remaining part of this article.
  4. using the new operator takes longer than the stack, because the compiler needs to determine a block of memory that can fit the declared new statement.

THE NEW OPERATOR

To declare the new operator, you write new followed by the data type. Once declared, the compiler determines the necessary size of the allocation in bytes.

int main() {
int* b = new int[50]; // allocates 200 bytes (50 * 4bytes)
}

From the properties, we mentioned that a new operator takes in size as an argument and returns a pointer to a memory address, which is similar to what the C malloc function does. The malloc function takes in the size of a data type and returns a pointer to memory block data type.

Let’s consider the following code.

int main() {
int a = 2;
int* b = new int[50]; // 200 bytes (50 * 4bytes)
// equivalent to
int* b = (int*)malloc(sizeof(int[50]));
}

The reason we can rewrite the new int[50] to (int*)malloc(sizeof(int[50])) is because usually for most compilers the new keyword or in the real sense the new operator simply takes in the size of the data type that it is declared on, which in this case is the int data type, which is 4 bytes of memory and returns a pointer to that block of memory. So basically, the new declare keyword takes in the size of the data type, similar to what the C malloc function does and returns a pointer to that allocated block of memory.

The only difference between this rewrite is that while the new keyword will take in the data type size and return the pointer to the block of memory received it will also call the constructor if a class is passed in as the data type. The equivalent code using malloc will not call the constructor if a class is passed in as the data type.

Although you can write the code in either way, you should not allocate memory in C++ code using the malloc function directly, although there are exceptions.

The new operator allocates memory on the heap, and unlike the stack, memory allocated on the heap does not exist within a scope but for the lifetime of a program. This means that once memory is allocated on the heap, it needs to be deleted (or cleaned up) from memory once it is no longer needed. This prevents memory leakage. This is the reason for the delete keyword. The delete keyword clean up memory that has been allocated on the heap.

THE DELETE OPERATOR

The delete keyword performs the memory cleanup. The delete keyword is also an operator in C++ that takes in a pointer to a block of memory and size in bytes to delete or cleanup. Just like the new keyword (or operator) calls the malloc function under the hood, the delete keyword (or operator) makes either of these calls under the hood:

  1. calls the free function, to free the block of memory that was “malloced”.
  2. calls the destructor function. The opposite of the constructor function.

Using the code example above, here’s how the delete keyword works.

int main() {
int a = 2;
int* b = new int[50]; // 200 bytes (50 * 4bytes)
delete[] b; // frees the `b` memory address
}

CONCLUSION

The new keyword allocates memory on the heap and expects that memory to be freed or cleaned up manually. This manual cleanup can lead to problems as the programmer can forget to clean up, because of this, there are other ways to automatically clean up data allocated on the heap. A simple method is the scoped based pointer method, and a more advanced methods like the reference counting method. Cleaning up memory prevents memory leakage which is the basics of bad code.

I hope you have learnt something from this article.

Comments and suggestions are allowed. Kindly drop them below.

Thanks for reading. 🙂

--

--

Mayowa Obisesan
Mayowa Obisesan

Written by Mayowa Obisesan

Entrepreneur | Computer Scientist

Responses (3)