
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
new and delete operator in C++
The new operator
The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.
Here is the syntax of new operator in C++ language,
pointer_variable = new datatype;
Here is the syntax to initialize the memory,
pointer_variable = new datatype(value);
Here is the syntax to allocate a block of memory,
pointer_variable = new datatype[size];
Here is an example of new operator in C++ language,
Example
#include <iostream> using namespace std; int main () { int *ptr1 = NULL; ptr1 = new int; float *ptr2 = new float(223.324); int *ptr3 = new int[28]; *ptr1 = 28; cout << "Value of pointer variable 1 : " << *ptr1 << endl; cout << "Value of pointer variable 2 : " << *ptr2 << endl; if (!ptr3) cout << "Allocation of memory failed\n"; else { for (int i = 10; i < 15; i++) ptr3[i] = i+1; cout << "Value of store in block of memory: "; for (int i = 10; i < 15; i++) cout << ptr3[i] << " "; } return 0; }
Output
Value of pointer variable 1 : 28 Value of pointer variable 2 : 223.324 Value of store in block of memory: 11 12 13 14 15
The delete operator
The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator.
Here is the syntax of delete operator in C++ language,
delete pointer_variable;
Here is the syntax to delete the block of allocated memory,
delete[ ] pointer_variable;
Here is an example of delete operator in C++ language,
Example
#include <iostream> using namespace std; int main () { int *ptr1 = NULL; ptr1 = new int; float *ptr2 = new float(299.121); int *ptr3 = new int[28]; *ptr1 = 28; cout << "Value of pointer variable 1 : " << *ptr1 << endl; cout << "Value of pointer variable 2 : " << *ptr2 << endl; if (!ptr3) cout << "Allocation of memory failed\n"; else { for (int i = 10; i < 15; i++) ptr3[i] = i+1; cout << "Value of store in block of memory: "; for (int i = 10; i < 15; i++) cout << ptr3[i] << " "; } delete ptr1; delete ptr2; delete[] ptr3; return 0; }
Output
Value of pointer variable 1 : 28 Value of pointer variable 2 : 299.121 Value of store in block of memory: 11 12 13 14 15
- Related Questions & Answers
- delete() operator in C++
- Difference between "new operator" and "operator new" in C++?
- Placement new operator in C++
- What is the difference between new/delete and malloc/ free in C/ C++?
- The new operator in Java
- The new operator in JavaScript
- How to delete a getter using the delete operator in JavaScript?
- How to delete a setter using the delete operator in JavaScript?
- How to initialize memory with a new operator in C++?
- delete() and free() in C++
- Delete and Earn in C++
- deque::operator= and deque::operator[] in C++ STL
- When to use new operator in C++ and when it should not be used?
- What is 'delete' Operator in JavaScript?
- How to work with delete operator in JavaScript?
Advertisements