使用和不使用新指针的区别
Difference between using and not using new pointer
请参考此处给出的代码:
这段代码是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。
内存是否自动释放,还是我必须在析构函数中释放?
每个对象 new
ed必须由您自己 delete
d。考虑使用智能指针,它们会为你处理这些事情。
以下几行:
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();
}
相关文章:
- 在 const 函数中通过引用和指针返回之间的区别
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 在 Rust 中,指针和引用有什么区别?
- 在以下声明中使用指针有什么区别?
- C++指针中的这两种类型的值分配有什么区别?
- 按值和引用传递指针之间的区别?
- printf() 和 std::cout 在指针方面的区别
- *++*++ppp,*++pp[1],*++(*(1+ppp)有什么具体的区别吗?(C/C++指针问题)
- 'new'和'='与指针的区别
- 使用指针向量和非指针向量有什么区别
- 函数指针和传递指针之间的区别
- C 中的通用指针和 C++ 中的通用指针有什么区别?
- 直接为浮点变量分配十六进制整数与通过指针转换分配之间的区别
- Fortran 中的指针和C++有什么区别
- 带"new"的指针和带"&variable"的指针有什么区别
- 指针声明和间接寻址之间的区别
- 在矢量的尖括号内使用指针符号与在外部使用指针符号有什么区别
- 在C++中,a+i 和 &a[i] 在指针算术方面有什么区别?
- ->运算符和指针 * 之间的区别 (C++)
- 当返回一系列字符或指针到char文字时的区别