C calling conventions and passed arguments

后端 未结 4 1002
-上瘾入骨i
-上瘾入骨i 2021-01-06 05:35

When making a function call in Linux (or OS X for that matter), can the callee modify the values of the arguments on the stack? I was under the assumption that since the ca

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 06:11

    If you pass by value:

    call do_it(to_it);
    

    The argument is copied (probably to the begining of the stack but maybe not depending on your compiler) the celled program can mess with this copy as much as it wants but the variable in the clling program will not be changed.

    If you pass by reference:

    call do_it(&to_it);
    

    Then the address of the variable is passed. Any changes the called variable makes will be to the original variable in the calling program.

提交回复
热议问题