简体   繁体   English

在返回(保持为空)C ++之后,指针不会返回到函数的引用

[英]pointer won't go back to it's reference from a function after return (stays null) C++

im using my own linked list made with stuct. 即时通讯使用我自己的链接列表与stuct。 The struct has 2 ints and 1 pointer to another struct, next. 结构有2个整数和1个指向另一个结构的指针,下一个。

I'm using the LL (linked list) but in one of the functions a certain pointer won't change. 我正在使用LL(链表),但在其中一个函数中,某个指针不会改变。

AddChain( &MakeChain(..values..), added )

this is how i call the function, i send her a new chain to link to the big one, and if there is no big one aka it's NULL so it will replace it. 这就是我调用这个函数的方法,我给她发了一个新的链来链接到一个大的,如果没有大的那个也叫它是NULL,所以它会取代它。 In this call added is a NULL ptr to a chain struct. 在这个调用中,添加了一个到链结构的NULL ptr。

void AddChain(PolyChain* pol, PolyChain* main) // adds the new piece to the chain
{
    PolyChain *current = main, *back = NULL;

    if (main == NULL)
    {
        pol->next = NULL;
        main = pol;
        return;
    }
... \\ there is continuation of the function but it wont get so far without main one
}

Now, as you can see if the main chain is a NULL I make to to reference to the same thing as the new chain I got. 现在,正如你可以看到主链是否为NULL,我指的是引用与我得到的新链相同的东西。 I'm running in the debugger and pol HAS a value and after the line: 我在调试器中运行和pol 具有价值和行之后:

main = pol;

main realy changes to point to what pol is pointing to. 主要的变化指向pol指向的东西。 BUT after the return in AddChain, added which is the main in AddChain is still a NULL. 但是在AddChain中返回后, 添加哪个是AddChain中的主要仍然是NULL。 It didn't get the value back from the function, it didn't change the pointer of added as like it was by value and not by reference, but it was by reference. 它没有从函数中获取值,它没有改变添加的指针,就像它是按值而不是通过引用,但它是通过引用。

What's causing this problem? 是什么导致了这个问题?

EDIT: MakeChain returns a PolyChain and AddChain gets PolyChain*, this is why i used &. 编辑:MakeChain返回PolyChain,AddChain得到PolyChain *,这就是我使用&的原因。

This happens because you pass your pointer PolyChain* main by value. 发生这种情况是因为您通过值传递指针PolyChain* main A simple way of visualizing "passing by value" is to think about it as "passing by copy": a copy of the pointer is made specifically for the call of AddChain . 可视化“按值传递”的一种简单方法是将其视为“通过副本传递”:指针的副本专门用于调用AddChain That is why any change to the pointer inside AddChain remains local to AddChain . 这就是为什么AddChain指针的任何更改仍然是AddChain本地。

You could fix this problem in three different ways: 您可以通过三种不同的方式解决此问题:

  • Take PolyChain*& main by reference - this is the simplest solution. PolyChain*& main参考 - 这是最简单的解决方案。 because nothing else needs to change. 因为没有别的东西需要改变。
  • Take PolyChain** main by pointer - in this case AddChain needs to dererefence main , ie use *main instead of main , and take a pointer when passing main to AddChain 指针PolyChain** main - 在这种情况下, AddChain需要dererefence main ,即使用*main而不是main ,并在将main传递给AddChain时取一个指针
  • Return PolyChain* with the new value of main - in this case the caller must make an assignment of the result back to the pointer passed for main . 返回PolyChain*同的新值main -在这种情况下,调用者必须确保结果的分配回被传为指针main

You are changing a copy of the pointer, since you are passing the pointer by value. 您正在更改指针的副本,因为您按值传递指针。 If you want to change the pointer that comes from the calling scope you will have to pass a pointer to the pointer or a reference to the pointer: 如果要更改来自调用范围的指针,则必须将指针传递给指针或指针的引用:

void AddChain(PolyChain*& pol, PolyChain*& main) {...}
void AddChain(PolyChain* pol, PolyChain* main)
...
        main = pol;
        return;

In that code, main is a local copy of whatever pointer was passed in, so assigning to main has no effect on the pointer outside the function. 在该代码中, main是传入的任何指针的本地副本,因此分配给main对函数外部的指针没有影响。

You didn't give enough information for me to deduce your intent, so it is likely (but not clear) that you could fix the problem by passing the pointer by reference: 你没有提供足够的信息来推断你的意图,所以很可能(但不清楚)你可以通过引用传递指针来解决问题:

void AddChain(PolyChain* pol, PolyChain*& main)

You must use pointer to pointer to modify pointer value inside the function 必须使用指针指针来修改函数内的指针值

void AddChain(PolyChain* pol, PolyChain** main)

than in your code: 比你的代码:

void AddChain(PolyChain* pol, PolyChain** main) // adds the new piece to the chain
{
    PolyChain *current = *main, *back = NULL;

    if (main == NULL)
    {
        pol->next = NULL;
        *main = pol;
        return;
    }
... \\ there is continuation of the function but it wont get so far without main one
}

to pass value to the function just call 将值传递给函数只是调用

AddChain(pol, &main);

where pol and main are regular pointers to PolyChain type. 其中polmainPolyChain类型的常规指针。

or with simple values: 或者使用简单的值:

PolyChain* pMain = &main;
AddChain(&pol, &pMain);

NOTE: This practics are extremaly error prone so you must be extra careful for managing pointers at this manner. 注意:这种实践极其容易出错,因此您必须格外小心地以这种方式管理指针。

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

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