Memory Management C++

In programming, arrays are often used to store multiple homogenous data. However, using arrays has serious drawbacks. A programmer should allocate memory for an array when it is declared. But it is next to impossible to determine the exact memory needed until run-time. To counter this hurdle, it is recommended that you declare an array with maximum possible memory required, (i.e. you should declare an array with maximum possible size expected). The only problem with this method is the unused memory is wasted and cannot be used by any other program.

In C++, we can avoid wastage of memory by dynamically allocating memory required during run-time. We can use the delete and new operators to do this. C++ dynamic memory allocation involves the programmer manually allocating memory.

If you are familiar with C programming, then you probably know that it uses malloc() and calloc(0 functions to allocate memory. Furthermore, the free() function is used to dynamically free memory. C++ also supports all these functions. Additionally, C++ programming language also has the new and delete operators. These two operators perform the task of allocating and freeing memory efficiently.

Dynamically allocated memory makes it possible to assign memory of variable size. This is not possible with the compiler allocated memory except for variable-length arrays. Also, it provides programmers with flexibility. They are free to allocate and deallocate memory at any time. Flexibility comes handy in cases where we are dealing with LinkedList, Tree, etc.

Please note:

Memory is automatically allocated and deallocated for normal variables like char str [5], int b, etc. For dynamically allocated memory, it is the work of the programmer to deallocate memory that is not in use. Failing to do this causes a memory leak.

In this article, we will only concentrate on the new and delete operators. If you want to learn about the malloc(), calloc() and free() functions, check out our article on memory management in C.You can also avail of our C++ programming assignment help and get assistance from our top-rated experts.

The new operator

The syntax used for this is:

Pointer – variable = new data_type;

Where;

  • Pointer- variable: The pointer of type data_type
  • Data_type: This can be any built-in data type. It includes arrays or any user-defined data types like class and structure

Initializing memory

The new operator can be used to initialize memory using the syntax below:

Pointer-variable = new data_type (value)

For example:

Int *p = new int (50);

Float *q = new float (30.25);

Allocating block of memory

We can also use the new operator to allocate an array or block of memory of type data_type. To do this, we use the syntax below:

Pointer-variable = new data_type [size];

Where

  • Size is a variable that specifies the number of elements in an array

For example

int *p = new int [6]

The example above allocates memory for 6 integers continuously of type int. Also, it returns a pointer to the first element of the sequence (assigned to p).

Int

p

p[0]        p[1]       p[2]    p[3]      p[4]   p[5]

where

  • P[0] is the first element, p[1] is the second element and so on.

Is there a difference between using the new operator and normal array declaration?

Yes, there is a difference between using the new operator to allocate a block of memory and declaring a normal array. The most notable difference is that normal arrays are deallocated by the compiler. However, you should note that dynamically allocated arrays always remain until the program terminates or they are deallocated by the programmer.

This operator denotes a request for memory allocation on the heap. It initializes the memory and returns the address of the newly allocated memory to the pointer variable. The new operator only does this if there is sufficient memory available.  If the memory is not enough, the new request indicates failure by throwing an exception.

Delete Operator

C++ provides the delete operator since programmers are tasked with the responsibility of deallocating dynamically allocated memory.

The syntax of the delete operator is:

delete pointer – variable;

Where:

  • Pointer-variable – the pointer that points to the data object created by the new operator

This syntax releases the memory pointed by pointer-variable.

For example

Delete I;

Delete j;

If you want to release the dynamically allocated array pointed by the pointer_variable, you can use the following form of the delete operator:

Delete [ ] pointer –variable;

The brackets tell us that an array is being deleted. They can be omitted if you are deleting a single object

For example:

Delete [ ] j; – This will free the entire array pointed by j.