Another c++ interview question
<p>One of the questions I was asked in past job interview. Interview is over now, so I am asking only not to screw up so bad on the next one..</p>

<pre><code>// Explain the differences between buffer1, buffer2 and buffer3 in the
// example code below. Consider:
// i.  Scope &amp; Lifetime
// ii. Performance &amp; use of system resources

char buffer1[512];

void func1()
{
    char buffer2[1024];
    //...
}

void func2()
{
    char* buffer3 = static_cast&lt;char*&gt; ( malloc(2048) );
    //...
}
</code></pre>

<p>My answer was..</p>

<p>*buffer1 is a global variable visible by entire application</p>

<p>buffer1 exists while application is running</p>

<p>buffer1 will use 512 bytes on stack</p>

<p>buffer1 is allocated on stack therefore its perfomance is much greater then buffer3</p>

<p>buffer2 is local variable created when func1 is called</p>

<p>buffer2 can only be accessed by members inside func1</p>

<p>buffer2 exists as long as func1 executes</p>

<p>buffer2 uses 1024 bytes on stack and has high performance due to being allocated on a stack</p>

<p>buffer3 is local variable created when func2 is called</p>

<p>buffer3 can only be accessed by members inside func2</p>

<p>buffer3 still exists after func2 finishes and needs to be freed manually</p>

<p>buffer3 uses 2048 of heap memory</p>

<p>buffer3 has slower performance then buffer1 or buffer2*</p>

<p>I am pretty sure I am missing something here, it is not entirely correct. Thanks for help guys!!</p>
