Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>For the first question:</strong> When the <code>new</code> operator is encounter by the compiler like in your example:</p> <pre><code>int * pData = new int[256]; </code></pre> <p>It effectively emits code which looks like this:</p> <pre><code>int *pData = reinterpret_cast&lt;int*&gt;(::operator new(256 * sizeof(int))); // the compiler may also choose to reserve extra space here or elsewhere to // "remember" how many elements were allocated by this new[] so delete[] // can properly call all the destructors too! </code></pre> <p>If a constructor should be called, that is emitted as well (in this example, no constructor is called I believe).</p> <p><code>operator new(std::size_t)</code> is a function which is implemented by the standard library, often, <strong>but not always</strong>, it will boil down to a <code>malloc</code> call.</p> <p><code>malloc</code> will have to make a <a href="http://en.wikipedia.org/wiki/System_call" rel="nofollow">system call</a>, to request memory from the OS. Since OS allocators usually work with larger fixed sized blocks of memory, <code>malloc</code> will not have make this call every time, only when it has exhausted the memory it currently has.</p> <hr> <p><strong>For the second question</strong>: For local variables, it really is up to the compiler. The standard makes no mention of a stack. However, most likely you are on a common architecture running a common OS and using a common compiler :-).</p> <p>So for the <strong>common</strong> case, the compiler will typically reserve space at the beginning of the function call for all local variables by adjusting the stack accordingly or reserving registers for them if it can (a register being the preferred choice since it is <strong>much</strong> faster).</p> <p>Then (in c++), when the variable is encountered, it will call the constructor. It could in theory, adjust the stack on an as needed basis, but this would be complicated to prove correct and less efficient. Typically reserving stack space is a single instruction, so doing it all at once is pretty optimal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload