Passing arguments to functions with const parameters: is it faster?

前端 未结 4 689
盖世英雄少女心
盖世英雄少女心 2021-02-07 00:32

Consider, for example:

int sum(int a, int b)
{
    return a + b;
}

vs.

int sum(const int a, const int b)
{
    return a + b;
}
         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 01:07

    Although late to the party, a compiler could put variables defined as const in a read-only memory segment/block, so that if an attempt was made to write to the address, via some pointer tomfoolery, the write to the memory would trigger an exception at runtime.

    -- Jamey

提交回复
热议问题