Simple new Memory Allocation Operator Example Program In C++
On this page (6sections)
About this program
This is an example program in c operator example programs. Read the concept first: Arithmetic Operators Example in C++, then study the code and output below.
Definition
- In C++, new Operator is used to allocating memory at runtime.
- The new operator demands memory allocation at runtime If enough memory is possible in the system. The new operator returns the address for newly allocated memory into the variable or object.
Syntax
data_type *ptr;
ptr = new data_type[size];
for example,
int *ptr;
ptr = new data_type[10];
new Operator for Memory Allocation Example Program
// Header Files
#include<iostream>
#include<conio.h>
#include<stdio.h>
//Main Function
using namespace std;
int a = 100; //Global variable
int main() {
// Local Variable Declaration
int num, *ptr, i;
cout << "Simple new Memory Allocation Example Program In C++\n";
cout << "Enter the Number for Dynamic Array : ";
cin >> num;
// Dynamic Array Created
ptr = new int[num];
cout << "Dynamic Array Created :" << num;
cout << "\nEnter Values for Dynamic Array :\n";
for (i = 0; i < num; i++)
cin >> ptr[i];
cout << "\nDynamic Array Values are\n";
for (i = 0; i < num; i++)
cout << ptr[i] << endl;
//Main Function return Statement
return 0;
}
Sample Output
Simple new Memory Allocation Example Program In C++
Enter the Number for Dynamic Array : 4
Dynamic Array Created :4
Enter Values for Dynamic Array :
100
450
300
120
Dynamic Array Values are
100
450
300
120
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- Arithmetic Operators Example in C++ — Tutorial — arithmetic, relational and logical operators.
- Simple Relational Operator Example Program In C++ — More in c operator example programs.
- Simple Logical Operators Example Program In C++ — More in c operator example programs.