My understanding is that, to allocate memory to a variable on stack, compiler needs to know its size during the compile time. In case of variable length arrays, as compiler can't figure out the size, I thought the memory is allocated in heap during run time. I printed the address to confirm this -
int main(){
int n;
cin>>n;
int A[n];
cout<< &n << " "<< A <<endl;
}
The output is
0x7ffc663c9b2c 0x7ffc663c9ae0
They are close to each other => A is on stack. How does the compiler know what size to allocate to array A at compile time?