title: C++ Variable Memory Allocation
body: <p>These are mostly compiler design questions. When your compiler compiles this, for example:</p>
  
  <p><code>int * pData = new int[256];</code></p>
  
  <p>How is the memory allocated on-the-fly? Does the compiler make a call to an OS routine that allocates memory for you or is a function compiled that allocates the memory for you?</p>
  
  <p>Also, when you write something like this:</p>
  
  <pre><code>if (x == 5)
      int y;
  </code></pre>
  
  <p>Since the memory is not allocated at runtime, I'm assuming that the data takes up some room in the data segment of the program. Since the compiler can't really determine whether or not the <code>int y;</code> branch will be executed at runtime, is the memory reserved for the variable whether or not <code>int y;</code> is executed? And if it's reserved regardless, isn't it more memory-efficient to mem alloc any variables that are in a block that may or may not be executed?</p>
  
  <p>o.o thanks</p>