
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
Create Dynamic Array of Integers in C++ Using New Keyword
In C++, a dynamic array can be created using new keyword and can be deleted it by using delete keyword.
Let us consider a simple example of it.
Example Code
#include<iostream> using namespace std; int main() { int i,n; cout<<"Enter total number of elements:"<<"\n"; cin>>n; int *a = new int(n); cout<<"Enter "<<n<<" elements"<<endl; for(i = 0;i<n;i++) { cin>>a[i]; } cout<<"Entered elements are: "; for(i = 0;i<n;i++) { cout<<a[i]<<" "; } cout<<endl; delete (a); return 0; }
Output
Enter total number of elements:7 Enter 7 elements 1 2 3 4 5 6 7 Entered elements are: 1 2 3 4 5 6 7
In this program, memory is allocated by declaring, int *a=new int(n), using new keyword. The occupied memory can be retrieved by calling delete (a).
Advertisements