简体   繁体   English

通过指针

[英]Passing by pointer

I am confused between these two functions: 我对这两个功能感到困惑:

void Swap_byPointer1(int *x, int *y){
    int *temp=new int;
    temp=x;
    x=y;
    y=temp;
}

void Swap_byPointer2(int *x, int *y){
    int *temp=new int;
    *temp=*x;
    *x=*y;
    *y=*temp;
}

Why Swap_byPointer2 succeeds to swap between x and y, and Swap_byPointer1 does not? 为什么Swap_byPointer2成功在x和y之间交换,而Swap_byPointer1没有成功?

In your first function you're swapping the pointers themselves, and in the second you're swapping the values of what the pointers point to, ie pointers dereferenced. 在第一个函数中,您要交换指针本身,而在第二个函数中,则要交换指针所指向的值,即指针被取消引用。

If you want to change what a pointer points to, you should pass a pointer to a pointer (ie int**x ) and change the second pointer. 如果要更改指针指向的内容,则应将指针传递给指针(即int**x )并更改第二个指针。

Like so 像这样

void Swap_byPointer1(int **x, int **y){
    int *temp;
    temp=*x;
    *x=*y;
    *y=*temp;
}

Initial setup of the functions, (common for both) (mock values) 功能的初始设置,(两者共同)(模拟值)

(Assumption : The values written outside the boxes are address. ) (假设: 写在框外的值为地址。

在此处输入图片说明

Function swap_byPointer1, 函数swap_byPointer1,

在此处输入图片说明

Function swapby_Pointer2, 函数swapby_Pointer2,

在此处输入图片说明

Hope this helped to get a picture of whats happening, Cheers! 希望这有助于了解发生的事情,干杯!

Because the parameters are passed by value. 因为参数是按值传递的。 In the first code sample all you do is swap local copies of the pointers. 在第一个代码示例中,您要做的只是交换指针的本地副本。 The second example actually writes to the pointees. 第二个示例实际上是写给指针对象。

Better still would be to use pass by reference and avoid heap allocation by using a stack allocated temp int. 更好的方法是使用按引用传递,并通过使用堆栈分配的temp int避免堆分配。

void SwapByRef(int &x, int &y)
{
    int temp=x;     
    x=y;     
    y=temp;
}
....
int x=1;
int y=2;
SwapByRef(x, y);

As others have pointed out, both of your code samples leak memory because temp is never deleted. 正如其他人指出的那样,您的两个代码示例都会泄漏内存,因为从不删除temp。 For a simple int like this, just use a stack allocated local int variable for your temp. 对于像这样的简单int,只需为您的温度使用堆栈分配的局部int变量。

The first snippet swaps the memory addresses which are the values of the pointers. 第一个代码片段交换作为指针值的内存地址。 Since the pointers are local copies, this has no effect for the caller. 由于指针是本地副本,因此对调用方无效。

Rewritten without a memory leak: 重写而没有内存泄漏:

void Swap_byPointer1(int *x, int *y){
    //e.g x = 0xDEADBEEF and y = 0xCAFEBABE;
    int *temp=x;
    x=y;
    y=temp;
    //now x = 0xCAFEBABE and y = 0xDEADBEEF
}

The second swaps the pointees (objects that the pointers point to). 第二个交换指针对象(指针指向的对象)。

Rewritten without a memory leak: 重写而没有内存泄漏:

void Swap_byPointer2(int *x, int *y){
    //e.g *x = 100 and *y = 200
    int temp =*x;
    *x=*y;
    *y=temp;
    //now *x = 200 and *y = 100
    //there are now different values at the original memory locations
}

(Pointers can point to dynamically allocated objects, but don't have to. Using a pointer does not mean there has to be a new -allocation. Pointers can point to objects with automatic lifetime as well.) (指针可以指向动态分配的对象,但不必这样做。使用指针并不意味着必须进行新的分配。指针也可以指向具有自动生存期的对象。)

The first function is just reassigning the local copy of pointers, not modifying the underlying values. 第一个功能只是重新分配指针的本地副本,而不修改基础值。 When it returns, it will have had no effect (other than allocating a new int) 当它返回时,它将没有任何作用(除了分配一个新的int以外)

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

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