开发者

C++ default argument value

开发者 https://www.devze.com 2023-01-06 02:06 出处:网络
Where does the compiler store default argument values in C++? global hea开发者_Python百科p, stack or data segment?

Where does the compiler store default argument values in C++? global hea开发者_Python百科p, stack or data segment?

Thanks Jack


They aren't necessarily stored anywhere. In the simplest case, the compiler will compile a function call exactly the same as if the missing arguments were present.

For example,

void f(int a, int b = 5) {
    cout << a << b << endl;
}

f(1);
f(1, 5);

The two calls to f() are likely compiled to exactly the same assembly code. You can check this by asking your compiler to produce an assembly listing for the object code.

My compiler generates:

    movl    $5, 4(%esp)    ; f(1)
    movl    $1, (%esp)
    call    __Z1fii

    movl    $5, 4(%esp)    ; f(1, 5)
    movl    $1, (%esp)
    call    __Z1fii

As you can see, the generated code is identical.

0

精彩评论

暂无评论...
验证码 换一张
取 消