简体   繁体   English

在函数调用之后,以下c ++函数中的数组重新分配似乎已被忽略。

[英]The array reassignment in the following c++ function seems to have been ignored after the function call.

void func(int* array)
{
    array=new int[5];
    for (int i=5; i>0; i--)
        array[5-i]=i;
}
int main()
{
    int array[5]= {1,2,3,4,5};
    func(array);
    cout<<array[1]<<endl;
}

I presume this has something to do with arrays being constant pointers. 我认为这与数组是常量指针有关。 How exactly does c++ handle this code and what happens to the dynamic memory that is assigned within the function? c ++如何处理此代码以及在函数中分配的动态内存会发生什么?

When you pass an array as argument to a function by value , it decays to the pointer to the first element, which then gets passed by value . 通过值传递数组作为参数传递给函数,它衰减到指针的第一个元素,然后把它由值来传递。 So what you recieve in the function func is a copy of the address of the first element of the array. 所以你在函数func收到的是数组第一个元素地址的副本 In the function, you simply change the address, which doesn't change the array in the main() function. 在函数中,您只需更改地址,这不会更改main()函数中的数组。 Whatever you do to array in func() , is local to that function only. 无论你对func() array做什么,只对该函数是本地的 It is like this: 它是这样的:

void f(int x)
{
     x= 100; //modifying the copy, not the variable in main()
}
int main()
{
     int value = 1000;
     f(x); //pass by value, means pass a copy!
}

In this case you're changing the value of x in f() . 在这种情况下,您将在f()更改x的值。 Likewise, in your case, you're changing the value of array . 同样,在您的情况下,您正在更改array的值。 Since array is a pointer, you're making it pointing to a different location by allocating a new memory to it. 由于array是指针,因此通过为其分配新内存使其指向不同的位置。 Every change is local to the function. 每个变化都是函数的本地变化。

Also note that since you allocate memory to the variable array , and don't deallocate it, your program leaks memory . 另请注意,由于您将内存分配给变量array ,并且不释放它,因此程序会泄漏内存 In order to avoid that, you must write: 为了避免这种情况,你必须写:

 delete [] array;

before returning from the function. 在从函数返回之前。 Again, this delete wouldn't change array in main() . 同样,这个delete不会改变main() array

By the way, if you want to change the element of the array in main() , then you should do this instead: 顺便说一句,如果你想在main()更改数组的元素,那么你应该这样做:

void func(int* array)
{
    //array=new int[5]; //just comment this
    for (int i=5; i>0; i--)
        array[5-i]=i; //now array points to the same memory 
                      //where main()'s array is in the memory.
}

Hope that helps. 希望有所帮助。

Here's the problem: 这是问题所在:

array=new int[5];

This assignment means that the passed parameter will not be modified. 此赋值意味着不会修改传递的参数。 After this assignment, array points to a newly allocated chunk of memory rather than to the array declared in main (and passed to func as an argument). 在这个赋值之后, array指向一个新分配的内存块,而不是指向main声明的array (并作为参数传递给func )。 What is modified is this newly allocated array -- and that's thrown away when the function returns. 修改的是这个新分配的数组 - 当函数返回时抛出它。 To make matters worse, you have a memory leak. 更糟糕的是,你有内存泄漏。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM