I was reading about dynamic memory allocation and static memory allocation and found the following about dynamic memory allocation:
In the programs seen in previous chapters, all memory needs were determined before program execution by defining the variables needed. But there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input.
So I wrote the following program in C++:
#include <iostream>
int main()
{
int n = 0;
int i = 0;
std::cout << "Enter size: ";
std::cin >> n;
int vector[n];
for (i=0; i<n; i++)
{
vector[i] = i;
}
return 0;
}
This program works. I don't understand how it works. When is the size determined here? How does the vector get allocated in this case?