
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Initialize Memory with New Operator in C++
In C++, the new operator is mainly used for allocating memory on the heap, but to initialize that memory, you need to explicitly declare and provide a value to it.
Here, the new operator dynamically allocates memory for a variable or object during runtime and returns a pointer to the allocated memory.
Here are the following ways you can initialize memory using the new operator:
new Operator in Built-in Types
The built-in types are the basic data types in C++, which define the type of data available, like numbers, characters, and Booleans etc.
Here, we will see how the new operator will dynamically allocate memory for these data types.
Syntax
Here is the following syntax for it.
type* pointer_name = new type; // Memory allocated (it will have garbage value) type* pointerName = new type(initialValue); // Memory allocated and initialized with value
Example
#include <iostream> using namespace std; int main() { int* intPtr = new int(100); float* floatPtr = new float(3.14); char* charPtr = new char('A'); cout << "Integer value: " << *intPtr << endl; cout << "Float value: " << *floatPtr << endl; cout << "Character value: " << *charPtr << endl; delete intPtr; delete floatPtr; delete charPtr; return 0; }
Output
Integer value: 100 Float value: 3.14 Character value: A
Explanation
In this code, the int, float, and character values have been initialized using the new keyword, which creates the memory for all three during the program's run time. And it is stored in the pointers intPtr, floatPtr, and charPtr, holding its memory addresses.
After printing the values stored in the memory, the program frees it with the delete keyword to prevent memory leaks.
new Operator in Array
The new operator can also be used to dynamically allocate memory for arrays.
Syntax
Here is the following syntax for it.
type* arrayPointer = new type[size]; // Memory allocated type* array_pointer = new type[size]{value}; // Memory allocated and values initialized
Example
#include <iostream> using namespace std; int main() { int* arr = new int[5]{0}; for (int i = 0; i < 5; ++i) { cout << "arr[" << i << "] = " << arr[i] << endl; } delete[] arr; return 0; }
Output
arr[0] = 0 arr[1] = 0 arr[2] = 0 arr[3] = 0 arr[4] = 0
Explanation
Here array has been initialized using the new operator of size 5 with all elements set to 0. In this, the memory allocated for all 5 int values provides different addresses for each and is stored in the pointer arr. After printing each element of the array, the delete[] arr; is been used to free the allocated memory and prevent memory leaks.
new Operator in Objects
The new operator is also used for objects of user-defined classes, where this memory allocates memory and calls the constructor of the class to initialize the object.
Syntax
class_name* obj = new class_name(); // Allocates memory and calls default constructor class_name* obj = new class_name(arg1, arg2); // Calls parameterized constructor
Example
#include <iostream> using namespace std; class Student { public: string name; int age; // Constructor Student(string n, int a) { name = n; age = a; } void display() { cout << "Name: " << name << ", Age: " << age << endl; } }; int main() { Student* s1 = new Student("Akansha", 22); s1->display(); delete s1; return 0; }
Explanation
Here new operator is used to create an object of the Student class, which allocates its memory on the heap and directly calls the parameterized constructor with the values initialized to the members.
The pointer s1 holds the address of the created object, which is accessed and printed using s1->display(). At last, delete s1; is used to free the allocated memory and to prevent memory leaks.