Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Any modern compiler can (and will) easily optimize out (i.e. inline) calls made through function pointers is situations when the compiler knows where the pointer is pointing to at compile time.</p> <p>In your specific example in general case the value of the pointer cannot be predicted at compile time at the point of the call, since it is passed from outside as a parameter of function <code>funct</code>. In situations when the function <code>funct</code> itself is small enough to get inlined, the run-time parameter gets eliminated and the whole code of <code>funct</code> gets "dissolved" in the <code>funct</code>s caller context. In the value of the pointer is known in that caller context, then, again, the compiler can easily eliminate the call through the pointer.</p> <p>Finally, if the function <code>funct</code> is relatively heavy (meaning it does not get inlined), the you should probably expect the compiler to generate an ordinary run-time call through the pointer from inside of <code>funct</code>. The potential for call elimination exists in this case as well, since the compiler can theoretically generate a separate version of <code>funct</code> for each compile-time value of <code>g</code>. For example, if you have</p> <pre><code>int funct(int x1, int x2, int x3, double (*f)(double)); </code></pre> <p>which is called with only two possible arguments for parameter <code>f</code></p> <pre><code>funct(rand(), rand(), rand(), g1); ... funct(rand(), rand(), rand(), g2); </code></pre> <p>then the compiler can "reduce" it into two functions</p> <pre><code>int funct_g1(int x1, int x2, int x3); int funct_g2(int x1, int x2, int x3); </code></pre> <p>without a function pointer parameters and with direct calls to either <code>g1</code> and <code>g2</code> inside. (Of course, this optimization is not in any way tied to function pointers and can be applied to any function parameter that is used with only a small fixed set of arguments. In fact, this is similar to C++ templates.) But I'm not aware of any compilers that would do anything like that.</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