Friday, 6 February 2015

Dynamic Memory in C++

Structure of the Problem Requirements 

Dynamic Memory mean your program allocate memory to your program according to the need and requirements of the program. In complicated problems memory has big concern and programmers can't assign static memory to the program. New operator is used to allocated dynamic memory. A data type is required for new to followed it. In this problem a dynamic array will be create according to the user input.

Source Code

#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,nums;
int * p;
cout << "How many numbers would you like to Enter ";
cin >> nums;
p= new (nothrow) int[nums];
if (p == 0)
cout << "Error! memory could not be allocated";
else
{
for (int i =0; i < nums; i++)
{
cout << "Enter number: ";
cin >> p[i];
}
cout<<endl;
cout << "You have entered: \n ";
for (int i =0; i <nums; i++)
cout <<endl<< p[i];
delete[] p;
}
return 0;
}

Output of the Program

Dynamic Memory in C++

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment