1int *p = new int; // request memory
2*p = 5; // store value
3
4cout << *p << endl; // Output is 5
5
6delete p; // free up the memory
7
8cout << *p << endl; // Output is 0
1#include <iostream>
2#include <string>
3
4using String = std::string;
5class Entity
6{
7private:
8 String m_Name;
9public:
10 Entity() : m_Name("Unknown") {}
11 Entity(const String& name) : m_Name(name) {}
12 const String& GetName() const {
13 return m_Name;
14 };
15};
16int main() {
17 // new keyword is used to allocate memory on heap
18 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
19 int* c = new int[50];
20 Entity* e1 = new Entity;//new keyword Not allocating only memory but also calling the constructor
21 Entity* e = new Entity[50];
22 //usually calling new will call underlined c function malloc
23 //malloc(50);
24 Entity* alloc = (Entity*)malloc(sizeof(Entity));//will not call constructor only allocate memory = memory of entity
25 delete e;//calls a c function free
26 Entity* e3 = new(c) Entity();//Placement New
1char* pvalue = NULL; // Pointer initialized with null
2pvalue = new char[20]; // Request memory for the variable
3
1#include <iostream>
2using namespace std;
3
4int main () {
5 double* pvalue = NULL; // Pointer initialized with null
6 pvalue = new double; // Request memory for the variable
7
8 *pvalue = 29494.99; // Store value at allocated address
9 cout << "Value of pvalue : " << *pvalue << endl;
10
11 delete pvalue; // free up the memory.
12
13 return 0;
14}