I'm new to c++ and I can't understand something in dynamic allocation. why does the following program build but gives an error and stops?
#include <iostream>
using namespace std;
int main()
{
int amount;
int *p = new int[amount];
cout << "enter the size of array" << endl;
cin >> amount;
for(int i = 0; i < amount ; i++)
{
cout << "enter the " << i + 1 << " number" << endl;
cin >> p[i];
}
for(int i = 0; i < amount ; i++)
{
cout << "number " << i + 1 << " is : " << p[i] << endl;
}
delete []p;
}
amounthas undefined value. Also note sincec++11explicit use ofnewis considered a bad practice. Just usestd::vectorin this code and ti will handle memory management for you.c++11explicit use ofnewis considered a bad practice" - use ofnew[]for arrays was discouraged in favor ofstd::vectorlong before C++11.