int main()
{
int num=0;
std::cin>>num;
int array[num];
array[0]=40;
std::cout<<array[0];
}
The above code is written in C++(The above code was written in C99)., and the code declares a variable and then declares an array with the size of the variable as input from the user. I know that such a declaration is a local variable, so an array called array is created in the stack area. However, since the stack area is a static area, I know that "the size of the stack frame should not change during runtime, and the size is already determined before the program starts", but in the case of my code, there is a contradiction that the size of the array is eventually determined during runtime by receiving input from the user after the program starts, so I wonder why an error does not occur. I know that C99 allows this type of thing, but in the end, what it means to say "allocated on the stack area" is that the size must already be specified before the program starts. Isn't that correct? But I'm also wondering why Java allows this behavior.
I used the C++ development version of Visual Studio 2022,
int main()
{
int a = 0
int b = 0;
std::cin >> b;
int array[b];
}
When typing this code
"Expression must have a constant value. The value of variable "b" cannot be used as a constant."
You will get an error. However, C99 says that code like the above does not raise an error.