- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a dynamic array of integers in C++ using the 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).
- Related Articles
- Using the new keyword in C#
- How to create a dynamic 2D array inside a class in C++
- How to create a dynamic 2D array in Java?
- How to create an array of integers in JavaScript?
- How to initialize a dynamic array in C++?
- Creating a JavaScript array with new keyword.
- What is the use of ‘new’ keyword in C#?
- How to create dynamic columns (headers) in CSV using PowerShell?
- How to create a new directory in Linux using the terminal?
- Create a new array from the masked array and return a new reference in Numpy
- How to create a new local group using PowerShell?
- How do I declare a 2d array in C++ using new
- What is the 'new' keyword in JavaScript to create an object and how to access values?
- how to initialize a dynamic array in java?
- How to create a new local user in windows using PowerShell?

Advertisements