Possible Duplicate:
C/C++: Array size at run time w/o dynamic allocation is allowed?
In the following listing, clearly size of buf
is determined by run-time constant j
.
How does compiler generate code to allocate storage on stack(not knowing value of j
at compile time)?
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
srandom(time(NULL));
int i = random();
cout<< "random number: "<<i<<endl;
if(i%2==0)
i=2;
else
i=1;
const int j=i;
char buf[j];
std::cout<<"size of buf array: "<<sizeof(buf)<<endl;
return 0;
}