分段故障(堆芯倾倒)?它是什么,为什么会引起???(c++中的双循环链表程序)
Segmentation fault (core dumped) ? what is it and why is caused ??? ( doubly circular linked list program in c++)
我的程序(它还不完整,但我真的很想知道我的错误):
#include <iostream.h>
using namespace std;
class dcllnode
{
private:
int data;
dcllnode *next;
dcllnode *prev;
public:
dcllnode(int ele)
{
data = ele;
prev = next;
next = prev;
}
friend class dcll;
};
class dcll
{
private:
dcllnode *head;
public:
dcll()
{
head = NULL;
}
void create();
void inserts(int ele);
void inserte(int ele);
void inserta(int ele, int pos);
void insertb(int ele, int pos);
void dels();
void dele();
void dela();
void display();
};
void dcll::create()
{
if (head == NULL)
{
head = new dcllnode(0);
cout << "list is created";
}
else
{
cout << "list has already been created";
}
}
void dcll::inserts(int ele)
{
if (head == NULL)
cout << "please create the list first ";
else
{
dcllnode *x;
x = new dcllnode(ele);
x->prev = head;
x->next = head->next;
(head->next)->prev = x;
head->next = x;
cout << "list is modified";
}
}
void dcll::inserte(int ele)
{
if (head == NULL)
cout << "please create the list first ";
else
{
dcllnode *x;
x = new dcllnode(ele);
x->next = head;
x->prev = head->prev;
(head->prev)->next = x;
head->prev = x;
cout << "list is modified";
}
}
void dcll::dels()
{
if (head == NULL)
cout << "please create the list first ";
else
{
dcllnode *x;
head->next = x;
head->next = x->next;
(x->next)->prev = head;
x->next = NULL;
x->prev = NULL;
delete(x);
cout << "list is modified";
}
}
void dcll::dele()
{
if (head == NULL)
cout << "please create the list first ";
else
{
dcllnode *x;
head->prev = x;
head->prev = x->prev;
(x->prev)->next = head;
x->prev = NULL;
x->next = NULL;
delete(x);
cout << "list is modified";
}
}
void dcll::display()
{
if (head == NULL)
cout << "please create the list first ";
else
{
dcllnode *p = head->next;
while (p != head)
{
cout << "n" << p->data;
p = p->next;
}
}
}
int main()
{
dcll l1;
l1.create();
l1.inserte(10);
l1.display();
l1.inserts(20);
return 0;
}
当我只使用create函数时,分割问题就不会发生。我想知道如何解决这些问题,以及如何避免未来出现分割错误。我们将提供任何信息和/或帮助。
使用非法指针(null或未初始化的指针,或指向未分配或非法内存的指针)时,通常会出现分段错误。
在您的情况下,很容易看出其中一个问题是什么,它在dcllnode
构造函数中进行
prev = next;
next = prev;
执行这些赋值时,指针next
和prev
未初始化,它们的值不确定。像这样将它们相互分配并不会初始化它们,只是使它们都具有相同的不确定值。稍后,当您取消引用这些指针时,您将有未定义的行为,从而导致分段错误和崩溃。
可能应该将两个指针初始化为null:
prev = nullptr; // or 0
next = nullptr; // or 0
[注意:在C++中不要使用NULL
,使用nullptr
(首选),或者如果没有,则使用普通0
。]
要完成约阿希姆的回答,在以下两个语句中:
(head->next)->prev = x;
(head->prev)->next = x;
head->next和head->prev可以是nullptr,所以在取消引用指针之前必须检查它。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址