Hi, I'm a novice programmer and I'm reading about Dynamic Memory Allocation from the cplusplus.com lessons and I copied the code from the page to play with it and practice. I built and ran the program, and I entered 999999999999 for how many numbers I wanted to enter. The result was "Enter a number:" flashing in my cmd window over and over very quickly like the matrix screensaver. It never gives any error message or stops.
My question is: what exactly is going on here and why? I'm just curious to learn the core of programming and have a good understanding.
// rememb-o-matic
#include <iostream>
#include <new>
usingnamespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
The problem is that 999999999999 is a big number for an int variable i dont remember what's the limit value for an int variable you should use some modifier like long.