Check out example codes for "new in c++". It will help you in understanding the concepts better.

Code Example 1

#include <iostream>
#include <string>

using String = std::string;
class Entity
	String m_Name;
	Entity() : m_Name("Unknown") {}
	Entity(const String& name) : m_Name(name) {}
	const String& GetName() const {
		return m_Name;
int main() {
  // new keyword is used to allocate memory on heap
	int* b = new int; // new keyword will call the c function malloc which will allocate on heap  memory = data and return a ptr to that plaock of memory
	int* c = new int[50];
	Entity* e1 = new Entity;//new keyword Not allocating only memory but also calling the constructor
	Entity* e = new Entity[50];
	//usually calling new will  call underlined c function malloc
	Entity* alloc = (Entity*)malloc(sizeof(Entity));//will not call constructor only  allocate memory = memory of entity
	delete e;//calls a c function free
	Entity* e3 = new(c) Entity();//Placement New

Code Example 2

//placement new in c++
char *buf  = new char[sizeof(string)]; // pre-allocated buffer
string *p = new (buf) string("hi");    // placement new
string *q = new string("hi");          // ordinary heap allocation
/*Standard C++ also supports placement new operator, which constructs 
an object on a pre-allocated buffer. This is useful when building a 
memory pool, a garbage collector or simply when performance and exception 
safety are paramount (there's no danger of allocation failure since the memory
has already been allocated, and constructing an object on a pre-allocated
buffer takes less time):

Code Example 3

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,

ordered_set ord_set;

int a;

Code Example 4

MyClass * p1 = new MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass))
      // and then constructs an object at the newly allocated space

  MyClass * p2 = new (std::nothrow) MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
      // and then constructs an object at the newly allocated space

  new (p2) MyClass;
      // does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
      // but constructs an object at p2

  // Notice though that calling this function directly does not construct an 
  MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
      // allocates memory by calling: operator new (sizeof(MyClass))
      // but does not call MyClass's constructor

  delete p1;
  delete p2;
  delete p3;

Code Example 5

// Release block of memory 
// pointed by pointer-variable
delete[] pointer-variable;  

   // It will free the entire array
   // pointed by p.
   delete[] p;

Code Example 6

#include <iostream>
using namespace std;

int main()
    int number;
        cin >> number;
            cout << "Not a number " << endl;
    cout << "number is " << number << endl;
    return 0;

Learn ReactJs, React Native from