What happens to a parameter passed by value that is modified locally?

后端 未结 5 378
粉色の甜心
粉色の甜心 2021-01-24 19:53

I am well aware that modifying a function\'s argument that is passed by value is ineffective outside of the C/C++ function, but compilers allow it - but what happens? Is a loca

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 20:26

    Value x is local and is kept in stack. Each function has it own part of stack which is reserved when program enters function. Value x will be located in this part of stack. And it is valid only in scope of function which defined it. If you change x it will be changed only for function where it is defined. if function ends (return from function), stack which was reserved for function is destroyed so also value x. So x won't be accessible because it doesn't exists anymore.

提交回复
热议问题