I'm fairly new to C++ so bear with me.
I have the following program to learn about dynamic memory allocation.
#include<iostream>
#include<new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to enter? ";
cin >> i;
p = new (nothrow) int [i];
if (NULL == p){
cout << "Not enough memory!";
}else{
for (n=0; n<i; n++){
cout << "Enter a number: ";
cin >> p[n];
}
cout << "You have entered: ";
for(n=0; n<i; n++){
cout << p[n] << ", ";
}
delete[] p;
}
return 0;
}
So long as a sensible amount is entered initially the program runs as expected. But when a huge number (1000000000000) is entered I expected the output "Not enough memory" when in fact it starts printing "Enter a Number: " presumably 1000000000000 times, obviously I haven't waited for output. Since this is in the "else" part of the check, why is this happening? I guessed that the comparison isn't working. Any help is appreciated. Thanks.