c++ - 为什么地址一样?

标签 c++ memory linked-list

这个问题在这里已经有了答案:





Why does the object created without using "new" operator gets the same address

(2 个回答)


1年前关闭。




why is the address of temp (in the while loop in main) the same everytime the loop runs i am trying to insert into a linked list and then display and then output the middle element but initially in displaying it ran an infinite loop only displayint the first element. On printing the address after inserting and llist.add_ele_to_beg(&temp); its priting the same address each time ! why is this happening ?


#include<iostream>
#include <unistd.h>

using namespace std;

class LinkedList;
class Node
{
    private:
    Node* next;
    int value;
    friend class LinkedList;
    public:
    Node(int ele) // constructor - declared in private section 
    // to prevent other classes creating objects of this class, 
    // only this class can create the object
    {
        next = nullptr;
        value = ele;
    }
};

class LinkedList
{
    private:
    Node* head;
    public:
    LinkedList()
    {
        head = nullptr;
    }
    void add_ele_to_beg(Node *temp)
    {
        // Node *temp = new Node(); // dynamically alloctg Node object
        // temp->value = x;
        temp->next = this->head;
        this->head = temp;
    }
    void display()
    {
        Node *h = this->head;
        while(h)
        {
            cout << h << endl;
            cout << h->value << endl;
            h = h->next; 
            cout << h << endl;
            cout << h->value << endl;
            exit(0);
        }
    }
    int findMiddle()
    {
        Node *fast, *slow = this->head;
        if(!slow)
        {
            return -1;
        }
        if(!slow->next)
        {
            return slow->value;
        }
        if(!slow->next->next)
        {
            return slow->value;
        }
        // n > 2
        fast = head->next->next;

        while(1)
        {
            slow = slow->next;
            if(!fast->next)
            {
                if(!fast->next->next)
                {
                    fast = fast->next->next;
                }
                else
                {
                    break;
                }   
            }
            else
            {
                break;
            }  
        }
        return slow->value;
    }
};

int main()
{
    LinkedList llist;
    int n;
    cout << "enter n" << endl;
    cin >> n;
    // create a dummy node
    cout << "enter elements to be inserted in the beg" << endl;
    int ele;
    while(n--)
    {
        cin >> ele;
        Node temp(ele); // obj node created and ctor initialises
        llist.add_ele_to_beg(&temp); // sending address of node to make change to 
        cout << &temp << endl;
        // node (passing by reference)
    }

    llist.display();

    cout << llist.findMiddle();
    cout << endl;
    return 0;
}

最佳答案

why is the address of temp (in the while loop in main) the same everytime the loop runs



因为您获得地址的对象具有自动存储期限。这意味着对象生命周期在它创建的 block 结束时结束(在你的情况下是循环结束),之后你有悬空指针。由于在对象生命周期结束后被认为是空闲的内存,编译器会出于实际目的再次重用相同的内存(它不是必须的,但它可以并且有意义)。

为了使其正常工作,您应该创建具有动态存储持续时间的对象,这意味着您可以控制对象的生命周期。您可以使用运算符 new为此,但最好使用智能指针而不是原始指针并让它管理对象生命周期。在这种情况下,您应该使用 std::make_uniquestd::make_shared取决于你想要什么样的所有权。您可以在此处找到有关操作的详细信息 C++ Linked list using smart pointers

关于c++ - 为什么地址一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62155041/

相关文章:

java - 复制构造函数断言错误

android - 在启动时启动 Qt 应用程序 - Android

c++在不知道扩展类的情况下调用存储在基类变量中的扩展类的构造函数

c - 派生一个不使用自己的内存副本的子进程

java - Java 应用程序运行时出现“无法分配内存”(errno=12) 错误

java - 将自己设置为 null - Java

c++ - 将二进制转换为十六进制 : error only on 2-7

c++ - 如何防止在 C++ 中重复随机生成的选项?

c - 如何使用 malloc() 在 redhat 中分配比 RAM 多的内存?

c - 错误 : unknown type name List when trying to create a Linked List