从 C++ 中的函数更改数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13295011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Altering an array from a function in C++
提问by joker
I am new to C++ (the usual intro for every newbie XD) and I found this unexpected behavior. I traced the variables and arrays in my program until I determined this pattern:
我是 C++ 新手(每个新手 XD 的常见介绍),我发现了这种意外行为。我跟踪了程序中的变量和数组,直到确定了这种模式:
#include <iostream>
using namespace std;
void showArray(int arr[], int n)
{
for(int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
}
void someFunction(int x[], int n) // changes the original values
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
}
void someFunction2(int * x, int n)
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
} // changes the original values
int someFunction3(int x[], int n)
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
return 0;
} // changes the original values
int someFunction4(int x[], int n)
{
x = new int[n];
x[0] = 2;
x[1] = 1;
x[2] = 0;
return 0;
} // does NOT change the original value
int main(void)
{
int * y = new int[3];
y[0] = 0;
y[1] = 1;
y[2] = 2;
showArray(y, 3);
someFunction4(y, 3);
showArray(y, 3);
return 0;
}
Why doesn't someFunction4()
change the array y
in main()
? When I call the other someFunctionX()
in main()
instead, y
successfully changes from {0, 1, 2}
to {2, 1, 0}
.
为什么不someFunction4()
改变数组y
的main()
?当我改为调用另一个someFunctionX()
时main()
,y
成功地从 更改{0, 1, 2}
为{2, 1, 0}
。
采纳答案by GraphicsMuncher
In someFunction4
, you assign x
to point to a new
array of integers, which you then assign. The array pointed to by the variable you passed into the function still points to the old array. The old array stays unchanged, since within someFunction4
you've set x
to reference a different array, namely the one you created in your function via new
.
在 中someFunction4
,您分配x
指向一个new
整数数组,然后分配该数组。您传递给函数的变量所指向的数组仍然指向旧数组。旧数组保持不变,因为someFunction4
您已在其中设置x
引用不同的数组,即您通过new
.
In order to make the original x
in someFunction4()
hold the values you assigned, do one of two things:
为了使原来x
在someFunction4()
保持你赋的值,做两件事情之一:
1) Get rid of x = new int[n];
. This will make someFunction4()
work like the previous ones.
1) 摆脱x = new int[n];
. 这将使someFunction4()
工作像以前的一样。
2) Pass a pointer to x
as an argument to someFunction4()
and have someFunction4()
take a pointer.
2) 将指向的指针x
作为参数传递给someFunction4()
并someFunction4()
获取指针。
int someFunction4(int *x[], int n)
{
*x = new int[n];
(*x)[0] = 2;
(*x)[1] = 1;
(*x)[2] = 0;
return 0;
} // Makes x point to a new a new array
And in your main, do
在你的主要工作中,做
someFunction4(&y,3);
回答by kevintodisco
In each of someFunction
, someFunction2
, and someFunction3
, you are actually passing a pointerto the memory you allocated for your array in main()
. This means that when you operate on the data this pointer points to:
在, 和 中的每一个中someFunction
,您实际上是在传递一个指向您为数组分配的内存的指针。这意味着当您对数据进行操作时,该指针指向:someFunction2
someFunction3
main()
x[1] = 1;
It actually affects the same memory that y
points to back in main()
!
它实际上影响了y
指向 back in的相同内存main()
!
However, in someFunction4
, you reassignthe pointer x
to point to new memory with the statement:
但是,在 中someFunction4
,您使用以下语句重新分配指针x
以指向新内存:
x = new int[n];
So it no longer points to the same memory that y
does in main()
, and any changes you make to it after that (but only within the scope of someFunction4
!) will not affect y
.
因此它不再指向与y
in相同的内存,main()
之后您对其进行的任何更改(但仅限于someFunction4
!)都不会影响y
.
回答by CyberGuy
I made a testcase.
我做了一个测试用例。
result
结果
0 1 2
0x943b008
0x943b018
0x943b008
0 1 2
Second one is the address is a address of new table. As you can see your pointer is locally pointing to other address.
第二个是地址是新表的地址。如您所见,您的指针在本地指向其他地址。
#include <iostream>
using namespace std;
void showArray(int arr[], int n)
{
for(int i = 0; i < n; i++) cout << arr[i] << " ";
cout << endl;
}
void someFunction(int x[], int n) // changes the original values
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
}
void someFunction2(int * x, int n)
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
} // changes the original values
int someFunction3(int x[], int n)
{
x[0] = 2;
x[1] = 1;
x[2] = 0;
return 0;
} // changes the original values
int someFunction4(int x[], int n)
{
x = new int[n];
std::cout << x << endl;
x[0] = 2;
x[1] = 1;
x[2] = 0;
return 0;
} // does NOT change the original value
int main(void)
{
int * y = new int[3];
y[0] = 0;
y[1] = 1;
y[2] = 2;
showArray(y, 3);
std::cout << y << endl;
someFunction4(y, 3) ;
std::cout << y << endl;
showArray(y, 3);
return 0;
}
回答by pm100
You have to understand how arguments are passed to functions
您必须了解如何将参数传递给函数
When you call SomeFunctionN(y,3) then inside SomeFunctionN the 'x' variable is a local variable initialized to point at the same array that y was pointing at in main.
当你调用 SomeFunctionN(y,3) 然后在 SomeFunctionN 内部,'x' 变量是一个局部变量,初始化为指向 y 在 main 中指向的同一个数组。
in SomeFunc4 you then create a new array and change you local pointer (x) to point at the new array. All changes you make hit the new array
在 SomeFunc4 中,您然后创建一个新数组并将本地指针 (x) 更改为指向新数组。您所做的所有更改都适用于新数组
In all other cases you leave x alone
在所有其他情况下,你不理会 x