简体   繁体   English

通过本地修改的值传递的参数会发生什么情况?

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

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? 我很清楚,在C / C ++函数之外,修改按值传递的函数参数无效,但是编译器允许这样做-但是会发生什么? Is a local copy made of the argument and that is modifiable within the function? 是否由参数构成本地副本,并且可以在函数中进行修改

#include <stdio.h>

void doSomething( int x )
{
    x = 42;
    printf( "The answer to Life, the Universe and Everything is (always): %i!\n", x );
}

int main( int argc, char **argv )
{
    int a = 0;
    doSomething( a );
    return -a;
}

Now this always exits without error, but where in the scheme of things (memory space) is the value represented in the function as x kept? 现在,这总是无错误地退出,但是在事物方案(内存空间)中函数中以x表示的值保留在哪里?

I imagine that should the (combined declaration and definition) begin: 我想(合并的声明和定义)应该开始:

void doSomething( const int x )

I would get my wrists slapped by any half-decent compiler. 我会被任何不正派的编译器打败。

For the function doSomething() , x is local to the function. 对于函数doSomething()x在函数本地。 It has the similar scope of any other variable defined at the beginning of the function body. 它具有与在函数体开头定义的任何其他变量类似的作用域。

In general terms, x exists only in the scope of doSomething() function. 一般而言, x仅存在于doSomething()函数的范围内。 x is defined once doSomething() is called (and the argument is passed) and destroyed once the control returns. 一旦doSomething()被调用(并且传递了参数doSomething()就定义了x并在控件返回后销毁x As long as the function call is being executed (ie, the variable remains in scope), the parameter(s) are the same as any other variable, only initialized by the arguments supplied in the function call. 只要执行了函数调用(即变量仍在作用域内),参数就与任何其他变量相同,仅由函数调用中提供的参数进行初始化。

Quoting C11 , chapter §6.2.1, Scopes of identifiers 引用C11 ,第§6.2.1章, 标识符范围

[...] If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. [...]如果声明标识符的声明符或类型说明符出现在功能定义的块内或参数声明列表中,则标识符具有块作用域,该作用域终止于相关块的末尾。 [...] [...]

As you are already aware, x being the local copy of the actual argument passed to function call, any changes made to x inside the function will not reflect into the caller (actual argument), but there's no reason for the compiler to complain as long as the operation(s) on x inside the function is (are) valid. 如您所知, x是传递给函数调用的实际参数的本地副本 ,对函数内部对x所做的任何更改都不会反映到调用方(实际参数)中,但是编译器没有理由抱怨因为该函数内部x上的操作有效。

A value parameter of a function is effectively a local variable of the function, except that it is initialised in the function call. 函数的value参数实际上是该函数的局部变量,但它是在函数调用中初始化的。 So these: 所以这些:

 void f( int n ) {
      n++;
   }

and: 和:

 void g() {
      int n = 0;
      n++;
   }

are effectively the same, if the call to f() was made as f(0). 如果对f()的调用设为f(0),则它们实际上是相同的。 In both cases, the variable will be discarded on function exit. 在这两种情况下,变量都将在函数退出时被丢弃。

形式参数x是在从实际的参数存储器中的单独的对象a ,所以任何更改x不会反映在a

As others have explained, x is local in function doSomething , any modifications only affect the local copy of the argument. 正如其他人解释的那样, xdoSomething函数中是局部的,任何修改仅影响参数的局部副本。

Note however that C++ allows passing by reference: a very small change in the definition of doSomething() would have significant consequences for this program: 但是请注意,C ++允许通过引用传递: doSomething()定义中的很小变化将对该程序产生重大影响:

void doSomething( int& x ) {
    x = 42;
    printf( "The answer to Life, the Universe and Everything is (always): %i!\n", x );
}

With the above function definition, variable a in main would indeed have its value changed to 42 . 使用上面的函数定义, main变量a实际上确实将其值更改为42 Since the code in main() is identical, this C++ feature can lead to confusing code, especially for a C programmer. 由于main()的代码相同,因此此C ++功能可能导致代码混乱,尤其是对于C程序员而言。

Value x is local and is kept in stack. 值x是局部的,并保留在堆栈中。 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. 值x将位于堆栈的这一部分。 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. 如果更改x,它将仅针对定义了x的函数进行更改。 if function ends (return from function), stack which was reserved for function is destroyed so also value x. 如果函数结束(从函数返回),则为函数保留的堆栈将被破坏,因此值x也将被破坏。 So x won't be accessible because it doesn't exists anymore. 因此x将不再可用,因为它不再存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果参数被传递两次,会发生什么情况? 一次按值,一次按引用? 会修改还是不修改? - What happens to a parameter if it is passed twice? Once by value and once by reference? Will it be modified or not? 当我在C ++中存储指向按值传递参数的指针时会发生什么? - What happens when I store a pointer to a parameter passed-by-value in C++? 当我返回传递给函数的引用参数时,内部会发生什么? - What happens internally when I return a reference parameter passed to a function? 将值传递给 C++ 中的运算符重载器 function 时会发生什么? - What happens when value is passed to operator overloader function in C++? 数组是通过引用传递的,但是,如果我仅传递未存储在内存中的数组的值,会发生什么? - Arrays are passed by reference, but what happens if I only pass the value of an array which is not stored in memory? 返回值会发生什么? - What happens with return value? 如果将 function 返回值传递给 function 引用参数,那么 C++ 会发生什么? - What happens in C++ if a function return value is passed to a function reference argument? 为什么在函数中未修改通过引用传递的参数? - Why is my parameter passed by reference not modified within the function? 当一个指针作为指向另一个函数内部的指针的指针传递时会发生什么? - what happens when a pointer is passed as a pointer to a pointer inside another function? 将传递引用变量分配给struct成员时会发生什么 - What happens when assigning passed-by-reference variable to struct member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM