run-time const to define array size
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed">C/C++: Array size at run time w/o dynamic allocation is allowed?</a>  </p>
</blockquote>



<p>In the following listing, clearly size of <code>buf</code> is determined by run-time constant <code>j</code>.
How does compiler generate code to allocate storage on stack(not knowing value of <code>j</code> at compile time)?</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;cstdlib&gt;

using namespace std;

int main(){
  srandom(time(NULL));    
  int i = random();
  cout&lt;&lt; "random number: "&lt;&lt;i&lt;&lt;endl;
  if(i%2==0)
    i=2;
  else
    i=1;
  const int j=i;
  char buf[j];
  std::cout&lt;&lt;"size of buf array: "&lt;&lt;sizeof(buf)&lt;&lt;endl;
  return 0;   
}
</code></pre>
