我试图理解使用值和引用将指针传递给函数之间的区别。就我而言,我正在“删除 []”传递的指针。我假设删除指针是对该指针进行修改的一种形式。因此,如果我按值将指向数组(比如 ptr)的指针传递给函数,则不应允许我在该函数内部“删除 [] ptr”。但是,当我编写这两种方法(按值和按引用传递 ptr)时,编译器允许我在这两种情况下删除函数内部的 ptr。
我很困惑,因为我认为我无法删除任何按值传递的指针。我在下面附加我的简单代码。有一个 related question在 Stack Overflow 上,但它没有回答我的问题,因为那里的 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";
}
最佳答案
operator delete
不会删除指针本身,它会删除指针指向的对象。因此,您可以根据需要创建任意数量的指针值拷贝,只要您记得在删除对象时丢弃所有这些值即可。
关于c++ - 按值和按引用传递函数指针的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8994991/