C++ linkedlist中指向指针的指针无法在每次调用主函数后保留指针。为什么? #包括 #定义打印(x)std::coutnext=*(此->头\u参考); 此->head\u ref=&new\u节点; } } void PrintLinkedList() { 节点*temp=(*(this->head_ref)); while(临时->下一步!=NULL) { 打印(温度->数据); } 打印(“\n”); } }; int main() { 链接列表a1; a1.插入式前端(5); a1.插入式前端(6); a1.插入式前端(7); a1.PrintLinkedList(); std::cin.get(); }

C++ linkedlist中指向指针的指针无法在每次调用主函数后保留指针。为什么? #包括 #定义打印(x)std::coutnext=*(此->头\u参考); 此->head\u ref=&new\u节点; } } void PrintLinkedList() { 节点*temp=(*(this->head_ref)); while(临时->下一步!=NULL) { 打印(温度->数据); } 打印(“\n”); } }; int main() { 链接列表a1; a1.插入式前端(5); a1.插入式前端(6); a1.插入式前端(7); a1.PrintLinkedList(); std::cin.get(); },c++,pointers,C++,Pointers,正如您将在watch1选项卡的屏幕截图中看到的那样,this->head\u ref保留节点的位置*,但节点*即(*(this->head\u ref))不保留节点的位置。我想知道是什么原因造成的。这是因为Node**head\u ref对内部指针指向节点的方式没有规定,还是范围问题?每次在下一个函数调用发生并且调试器进入insertFront函数后,问题都会发生,节点*成为一个不指向任何内容的自由指针 答案由@IgorTandetnik在评论中提供 节点对象在堆上分配。但是名为new\u

正如您将在watch1选项卡的屏幕截图中看到的那样,
this->head\u ref
保留
节点的位置*
,但
节点*
(*(this->head\u ref))
不保留节点的位置。我想知道是什么原因造成的。这是因为
Node**
head\u ref对内部指针指向节点的方式没有规定,还是范围问题?每次在下一个函数调用发生并且调试器进入
insertFront
函数后,问题都会发生,
节点*
成为一个不指向任何内容的自由指针


答案由@IgorTandetnik在评论中提供


节点
对象在堆上分配。但是名为
new\u Node
Node*
对象是在堆栈上分配的。您有
head\u ref
指向
new\u节点
new\u节点
指向
节点
对象。然后函数返回,
new\u node
消失,并且
head\u ref
和堆分配的节点之间不再存在任何连接
head\u ref
指向某个垃圾值,该值以前是
new\u节点
,堆上的
节点
由于不再指向它而泄漏

我使用
Node*head\u ref
相应地更改了代码,它将引用分配给
Node
的堆,而不是超出范围的
new\u Node
指针

#include<iostream>

#define print(x) std::cout<<x<<std::endl;

class Node
{
    public:
    int data;
    Node* next;
};

class LinkedList
{
private:
    Node** head_ref;
public:
    LinkedList() :head_ref(NULL) {};



    void insertFront(int new_data)
    {
        Node* new_node = new Node();
        new_node->data = new_data;

        if(this->head_ref == NULL)
            this->head_ref = &new_node;
        else
        {
            new_node->next = *(this->head_ref);
            this->head_ref = &new_node;
        }
    }

    void PrintLinkedList()
    {
        Node* temp = (*(this->head_ref));
        while (temp->next != NULL)
        {
            print(temp->data);
        }
        print("\n");
    }
};

int main()
{
    LinkedList a1;
    a1.insertFront(5);
    a1.insertFront(6);
    a1.insertFront(7);
    a1.PrintLinkedList();
    std::cin.get();
}
#包括
#定义打印(x)标准::coutnext=this->head\u ref;
此->头\u参考=新的\u节点;
}
}
void PrintLinkedList()
{
节点*temp=this->head\u ref;
while(临时->下一步!=NULL)
{
打印(温度->数据);
温度=温度->下一步;
}
打印(温度->数据);
打印(“\n”);
}
};
int main()
{
链接列表a1;
a1.插入式前端(5);
a1.插入式前端(6);
a1.插入式前端(7);
a1.PrintLinkedList();
std::cin.get();
}

将局部变量的地址分配给
head\u ref
new\u节点
。然后函数返回,局部变量被销毁,
head\u ref
结束一个悬空指针;以后使用它的任何尝试都会显示未定义的行为。为什么要将
头\u ref
设为
节点**
?这没有意义。@IgorTandetnik但是新的\u节点是为此目的而分配的,对吗?或者堆分配也发生在类的范围内?(是的,我已经用Node*head_ref制作了这个程序,它可以工作了,我只是想用一个指向指针的指针来测试自己。)问题是当调试值时,它在类外时不会丢失(这是令人困惑的部分),如屏幕截图所示,当调试器重新进入insertFront函数时,它将丢失。
节点
对象在堆上分配。但是名为
new\u Node
Node*
对象是在堆栈上分配的。您有
head\u ref
指向
new\u节点
new\u节点
指向
节点
对象。然后函数返回,
new\u node
消失,并且
head\u ref
和分配的堆
节点之间不再存在任何连接
head\u ref
指向某个垃圾值,其中
new\u node
过去是,堆上的
node
被泄漏,因为再也没有任何东西指向它了。@IgorTandetnik哦,好吧,就是这样。谢谢,那么当您分配指针时,地址位置在堆栈上,值在堆上?是吗?我不确定我是否理解这个问题。当您获取堆栈上局部变量的地址时,很明显,结果指针指向堆栈上的该变量。当您使用
new
在堆上分配一个对象时,结果指针显然指向堆上的该对象。
#include<iostream>
#define print(x) std::cout<<x<<std::endl;

class Node
{
public:
    int data;
    Node* next;
};

class LinkedList
{
private:
    Node* head_ref;
public:
    LinkedList() :head_ref(NULL) {};

    void insertFront(int new_data)
    {
        Node* new_node = new Node();
        new_node->data = new_data;

        if(this->head_ref == NULL)
            this->head_ref = new_node;
        else
        {
            new_node->next = this->head_ref;
            this->head_ref = new_node;
        }
    }

    void PrintLinkedList()
    {
        Node* temp = this->head_ref;
        while (temp->next != NULL)
        {
            print(temp->data);
            temp = temp->next;
        }
        print(temp->data);
        print("\n");
    }
};

int main()
{
    LinkedList a1;
    a1.insertFront(5);
    a1.insertFront(6);
    a1.insertFront(7);
    a1.PrintLinkedList();
    
    std::cin.get();
}