通过值和引用将指针传递给函数之间的区别

时间:2012-01-24 22:10:31

标签: c++ pass-by-reference pass-by-value delete-operator

我试图理解使用值和引用传递指向函数的指针之间的区别。在我的情况下,我'删除[]传递指针。我假设删除指针是一种修改该指针的形式。因此,如果我通过值将指向数组(比如ptr)的指针传递给函数,我不应该被允许在该函数内部'删除[] ptr'。当我编写这两种方式(通过值和引用传递ptr)时,编译器允许我在两种情况下删除函数内部的ptr。

我很困惑,因为我认为我无法删除任何通过值传递的指针。我在下面附上我的简单代码。 Stack Overflow上有一个related question,但它没有回答我的问题,因为OP没有兴趣修改函数内的指针。

// Understanding passing by value and reference for pointers

#include<iostream>

using namespace std;

int* createNew_value(int* _oarr, int &_size);
// createNew_value() appends an integer to the input integer array _oarr. The
// value of _size is incremented by one after the call to createNew_value. The
// pointer to array _oarr is passed by value.

int* createNew_reference(int* &_oarr, int &_size);
// Same functionality as createNew_value(), but here the pointer to array _oarr
// is passed by reference.

void displayIntArray(int* _iarr,int _size);
// Just diplays elements of the passed integer array.

int main()
{
    int *int_array;
    int size;

    cout << "Enter the number of elements:";
    cin >> size;

    int_array = new int [size];

    // Initialize elements of array to consecutive integers. This initialization
    // is only here to ensure that the elements of array are not undefined.
    // Other than that this initialization doesnt serve any purpose

    for (int j = 0; j <= size - 1; j++)
        int_array[j] = j;

    // Display the starting location and elements of the filled array;    
    cout << "[main()]: int_array [before createNew_value()] = " << int_array << endl;
    displayIntArray(int_array,size);

    // Display the starting location and elements of the filled array, after
    // call to createNew_value().

    int_array = createNew_value(int_array, size);

    cout << "[main()]: int_array [after createNew_value()] = " << int_array << endl;
    displayIntArray(int_array,size);

    // Display the starting location and elements of the filled array, after
    // call to createNew_reference().

    int_array = createNew_reference(int_array, size);

    cout << "[main()]: int_array [after createNew_reference()] = " << int_array << endl;
    displayIntArray(int_array,size);

    // Finally delete int_array to prevent memory leak. 
    delete [] int_array;

    return(0);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int* createNew_value(int* _oarr, int &_size)
// createNew_value() accomplishes the following: It creates a pointer to an
// integer array, called temp, and allocates enough memory for storing (_size +
// 1) elements. It then copies the elements of _oarr into temp, and appends one
// more integer to temp. It then deletes the original array pointer _oarr and
// returns temp. The return value of this function is a pointer to an array with
// one element larger than the input array
{
    int* temp;

    temp = new int [_size + 1];

    //copy elements of old array, _oarr, into temp

    for(int i = 0; i <= _size - 1; i++)
        temp[i] = _oarr[i];

    temp[_size] = temp[_size - 1] + 1;

    _size++;

    cout << "[createNew()]: _oarr = " << _oarr << endl;
    cout << "[createNew()]: temp = " << temp << endl;

    delete [] _oarr;

    // Since _oarr is passed by value, C++ SHOULDNT allow me to delete[] it !!

    // QUESTION: I am passing _oarr by value here. So why does C++ allow me to
    // delete [] it? Isnt deleting equivalent to modification? If yes, how can I
    // modify _oarr if I am passing it my value?

    return(temp);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int* createNew_reference(int* &_oarr, int &_size)
// createNew_reference() accomplishes the following: It creates a pointer to an
// integer array, called temp, and allocates enough memory for storing (_size +
// 1) elements. It then copies the elements of _oarr into temp, and appends one
// more integer to temp. It then deletes the original array pointer _oarr and
// returns temp. The return value of this function is a pointer to an array with
// one element larger than the input array
{
    int* temp;

    temp = new int [_size + 1];

    //copy elements of old array, _oarr, into temp

    for(int i = 0; i <= _size - 1; i++)
        temp[i] = _oarr[i];

    temp[_size] = temp[_size - 1] + 1;

    _size++;

    cout << "[createNew()]: _oarr = " << _oarr << endl;
    cout << "[createNew()]: temp = " << temp << endl;

    delete [] _oarr;
    return(temp);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void displayIntArray(int* _iarr,int _size)
{
    cout << "{ ";

    for (int n = 0; n <= _size - 2; n++)
        cout << _iarr[n] << ", ";

    cout << _iarr[_size - 1] << " }\n";
}

2 个答案:

答案 0 :(得分:1)

operator delete不会删除指针本身,它会删除指针所指向的对象。因此,只要您记得在删除对象时抛弃所有这些值,就可以创建任意数量的指针值。

答案 1 :(得分:0)

考虑指针的最佳方式就像一杯水。如果你传递一个指针/参考,那么只有一杯水可以对杯子进行任何改变,就像添加食物的颜色会发生在使用那个杯子的任何东西上。然而,通过价值就像创造第二杯相同的水,但是如果你在那个杯子里添加食用色素,原来的杯子仍然是清澈的。

通常,当您通过引用传递时,您希望影响内存中的对象,而无需将其返回到调用函数。在通过值传递的情况下,您可以将其返回到上一个函数并重写上一个对象。

这有助于您更好地理解它吗?