Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>What is the difference between an ordinary function and a static function "in terms of memory allocation only" ?</p> </blockquote> <p>Nothing. A static function is just like a global function except for the scope.</p> <p>Even for non-static member functions, there's no extra memory required. The member function <code>int C::f(int arg1, int arg2)</code> is just syntactic sugar for something like <code>int C__f(C* this, int arg1, int arg2)</code>.</p> <blockquote> <p>What if the member function contains some local variables ? In that case the function "should" have a separate copy of that variable - specific to the object invoking the function...</p> </blockquote> <p>There's a copy of the local variables for <em>each invocation</em> of the function (unless they're <code>static</code>). This is why recursion in possible in C++.</p> <blockquote> <p>How is this problem solved in C++ ?</p> </blockquote> <p>Function calls are based around "stack frames". A stack frame consists of:</p> <ul> <li>The arguments to the function (including the implicit <code>this</code> if applicable).</li> <li>Any other non-<code>static</code> local variables in the function.</li> <li>The "return address", which tells the processor where to resume execution once the function is done.</li> </ul> <p>Whenever a function is called, a stack frame is created. When the function is, the stack frame is destroyed. If a function is called recursively, each level of recursion gets its own stack frame. For example, if you have</p> <pre><code>int factorial(int n) { if (n &lt;= 1) return 1; else return factorial(n - 1) * n; } </code></pre> <p>Then when you call <code>factorial(3)</code>, a stack frame gets created like so:</p> <pre><code>------------------------ stack pointer (SP) n = 3 RA = &lt;in main()&gt; </code></pre> <p>When the recursive call is made to <code>factorial(2)</code>, an additional frame is added to the top of the stack</p> <pre><code>------------------------ SP n = 2 RA = &lt;in factorial()&gt; ------------------------ n = 3 RA = &lt;in main()&gt; </code></pre> <p>Another recursive call is made, to <code>factorial(1)</code>.</p> <pre><code>------------------------ SP n = 1 RA = &lt;in factorial()&gt; ------------------------ n = 2 RA = &lt;in factorial()&gt; ------------------------ n = 3 RA = &lt;in main()&gt; </code></pre> <p>This is the base case for the recursion, and the return value of 1 is stored in a register. The function call being complete, the top stack frame is destroyed, and execution continues at saved return address.</p> <pre><code>------------------------ SP n = 2 RA = &lt;in factorial()&gt; ------------------------ n = 3 RA = &lt;in main()&gt; </code></pre> <p>Now, the call to <code>factorial(2)</code> can compute its return value (2), and another stack frame can be destroyed:</p> <pre><code>------------------------ SP n = 3 RA = &lt;in main()&gt; </code></pre> <p>Finally, we can compute the result of the original function call (6), and destroy this stack frame too.</p>
 

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