Search code examples
cfunctioninline

I have query on inline fuction


When inline function gets executed, which memory area they will use

Let me give some example int sum(int a,int b) {return a+b; } Where the variables a and b are stored ?


Solution

  • Most likely no memory is used at all. If the arguments of sum() are in registers at the time of the "call", then they will simply be added and the sum put in another register, or one of those registers if the contents are not needed after that.

    For example, this:

    inline int sum(int a, int b) {
        return a + b;
    }
    
    int main(int argc, char **argv) {
        (void)argv;
        int a = argc;
        int b = a * 5;
        return sum(a, b);
    }
    

    Compiles to this:

    _main:                                  ; @main
        .cfi_startproc
    ; %bb.0:
        add w8, w0, w0, lsl #1
        lsl w0, w8, #1
        ret
        .cfi_endproc
    

    All done in the registers. Nothing stored in memory. (And the optimizer added instead of multiplied.)