×

Search anything:

new operator in C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes

The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.

Syntax :

pointer-variable = new data-type;

Initialize memory:

We can also initialize the memory using new operator:

pointer-variable = new data-type(value);

Example:

int *p = new int(25);
float *q = new float(75.25); 

Allocate block of memory:

new operator is also used to allocate a block(an array) of memory of type data-type.

pointer-variable = new data-type[size];

where size(a variable) specifies the number of elements in an array.

Example:

int *p = new int[10]

Dynamically allocates memory for 10 integers continuously of type int and returns pointer to the first element of the sequence, which is assigned to p(a pointer). p[0] refers to first element, p[1] refers to second element and so on.
dynamic

Normal Array Declaration vs Using new

There is a difference between declaring a normal array and allocating a block of memory using new. The most important difference is, normal arrays are deallocated by compiler (If array is local, then deallocated when function returns or completes). However, dynamically allocated arrays always remain there until either they are deallocated by programmer or program terminates.

How does memory gets allocated

Case 1: Using "new" to allocate an array, as in

int* Array = new int[10];

In this case, each element of foo will be in contiguous virtual memory.

topic of image

Case 2: Using consecutive "new" operations non-atomically, as in

int* foo = new int;
int* bar = new int;

In this case, there is never a guarantee that the memory allocated between calls to "new" will be adjacent in virtual memory.

topic of image

What if enough memory is not available during runtime?

If enough memory is not available in the heap to allocate, the new request indicates failure by throwing an exception of type std::bad_alloc, unless “nothrow” is used with the new operator, in which case it returns a NULL pointer Exception handling of new operator . Therefore, it may be good idea to check for the pointer variable produced by new before using it program.

int *p = new(nothrow) int;
if (!p)
{
   cout << "Memory allocation failed\n";
}

Difference between new and malloc

  1. new calls constructor while malloc doesnot calls constructors.
  2. new is an operator while malloc is a function
  3. new returns exact data type while malloc returns void *
  4. On failure new throws while malloc returns NULL
  5. Memory allocated from free store in new while memory allocated is from heap in malloc.
  6. new can be overridden and malloc cannot be overridden
  7. size is calculated by compiler for new while size is calculated manually in malloc.

Question :

Consider the following C++ program:

#include <iostream>
using namespace std;
 
class Test 
{
  int x;
  Test() { x = 5;}
};
 
int main()
{
   Test *t = new Test;
   cout << t->x;
}

What will be the output of the above C++ program?

Compilation error
5
Garbage value
0
There is compiler error: Test::Test() is private. new makes call to the constructor. In class Test, constructor is private (note that default access is private in C++).
Harshita Sahai

Harshita Sahai

Maintainer at OpenGenus | Previously Software Developer, Intern at OpenGenus (June to August 2019) | B.Tech in Information Technology from Guru Gobind Singh Indraprastha University (2017 to 2021)

Read More

Improved & Reviewed by:


OpenGenus Tech Review Team OpenGenus Tech Review Team
new operator in C++
Share this