使用和不使用新指针的区别

Difference between using and not using new pointer

本文关键字:区别 指针 新指针      更新时间:2023-10-16

请参考此处给出的代码:

这段代码是c++中栈实现的一部分:

代码1:

void Stack::pop()
{
    if (top != 0) {
        node* temp = top;
        top = top -> link;
        delete temp;
    }
}
代码2:

void Stack::pop()
{
    if (top != 0) {
        node* temp = new node;
        temp = top;
        top = top -> link;
        delete temp;
    }
}

在第一个例子中,我没有使用new,而在第二个例子中我使用了它。在运行时,两者给出相同的输出和完整的程序,可以在下面找到:

#include <iostream>
using namespace std;
struct node {
    string name;
    node* link;
};
class Stack
{
    node* top;
public:
    Stack();
    void push(string s);
    void pop();
    void display();
    ~Stack(){}
};
Stack::Stack() {
    top = 0;
}
void Stack::push(string s)
{
    node* temp = new node;
    temp -> name = s;
    temp -> link = top;
    top = temp;
}
void Stack::pop() // Function in question
{
    if (top != 0) {
        node* temp = new node;
        temp = top;
        top = top -> link;
        delete temp;
    }
}
void Stack::display()
{
    node* temp = new node;
    temp = top;
    while (temp != 0)
    {
        cout << temp -> name << "n";
        temp = temp -> link;
    }
}
int main() {
    Stack s;
    s.push("Ra");
    s.push("Sa");
    s.push("Ga");
    s.pop();
    s.display();
}
在这里使用和不使用new指针的区别是什么?

内存是否自动释放,还是我必须在析构函数中释放?如果有,该怎么做?

在第二个代码片段中存在内存泄漏,尽管它看起来运行良好。new node对于node* temp = new node;来说是没有意义的,因为temp是同时分配给top的。然后new node创建的原始内存地址丢失,不能再delete d。

内存是否自动释放,还是我必须在析构函数中释放?

每个对象 newed必须由您自己 deleted。考虑使用智能指针,它们会为你处理这些事情。

以下几行:

    node* temp = new node;
    temp = top;

分配新节点,将其存储在temp变量中,并在下一个变量中存储另一个指针。这样新节点就会丢失,并使用新的节点。node* temp = new node;除了内存泄漏之外没有任何影响。

内存是否会自动释放,还是我必须这样做在析构函数?

。内存不会自动释放。而且你几乎从来不会手动调用对象析构函数。

如果有,怎么做?

旧的方法是使用delete。但是在现代c++中,你不应该使用裸指针,而应该考虑使用std::unique_ptr

在使用new/分配内存时,您正在导致内存泄漏。

node* temp = new node;
temp = top; //temp is now pointing to a new memory location.
            //Thus the memory allocated by in the previous code line gets leaked

Code1是正确的方法。代码2导致内存泄漏。

必须使用析构函数中的delete操作符删除分配的内存。

Stack::~Stack()
{
    while(NULL != top)
    {
        node* temp = top;
        top = top->link;
        delete temp;
    }
}

智能指针解决方案。下面的代码需要c++11编译器才能编译/工作。

#include <iostream>
#include <memory>
using namespace std;
struct node {
    string name;
    std::unique_ptr<node> link;
};    
typedef std::unique_ptr<node> node_ptr;
class Stack
{
    node_ptr top;
public:
    Stack();
    void push(string s);
    void pop();
    void display();
    ~Stack(){}
};
Stack::Stack() {
}
void Stack::push(string s)
{
    auto temp = std::make_unique<node>();
    temp -> name = s;
    temp -> link = top;
    top = temp;
}
void Stack::pop()
{
    if (top != null) {
        top = top -> link;
    }
}
void Stack::display() const
{
    node* temp = top.get();
    while (temp != 0)
    {
        cout << temp -> name << "n";
        temp = (temp -> link).get();
    }
}
int main() {
    Stack s;
    s.push("Ra");
    s.push("Sa");
    s.push("Ga");
    s.pop();
    s.display();
}