我在具有不同版本的GNU的不同机器上测试了此代码段,只有一台Clang 1001.0.46.3奇怪的Mac报告分段错误。在这段代码中是否存在地址问题或指针问题?
#include <iostream>
#include <vector>
using namespace std;
class Node
{
public:
Node* next;
int val;
};
class List
{
private:
Node* head;
Node* tail;
public:
List()
{
head = tail = nullptr;
}
bool isEmpty()
{
if(!head && !tail) return true;
return false;
}
void pushBack(int num)
{
Node* newNode = new Node;
newNode->val = num;
if(isEmpty()) head = tail = newNode;
else
{
tail->next = newNode;
tail = tail->next;
}
}
void pushFront(int num)
{
Node* newNode = new Node;
newNode->val = num;
if(isEmpty()) head = tail = newNode;
else
{
Node* tmp = head;
newNode->next = tmp;
head = newNode;
}
}
void popBack()
{
if(head == tail) {delete head; head = nullptr; return;}
Node* tmp = head;
while(tmp->next != tail) tmp = tmp->next;
tail = tmp;
delete tmp->next;
tmp->next = nullptr;
}
void popFront()
{
if(head == tail) {delete head; head = nullptr; return;}
Node* tmp = head;
tmp = tmp->next;
delete head;
head = nullptr;
head = tmp;
}
int findLen()
{
if(isEmpty()) return 0;
int len = 0 ;
Node* tmp = head;
while(tmp)
{
len++;
//if(tmp == tail) break;
tmp = tmp->next;
}
return len;
}
void inserter(int position,int num)
{
if(position > findLen() || isEmpty()) return;
int index = 0;
Node* tmp = head;
Node* newNode = new Node;
newNode->val = num;
if(position == 0) {pushFront(num); return;}
else if(position == findLen()) {pushBack(num); return;}
while(tmp->next)
{
index++;
if(index == position)
{
Node* tmp2 = tmp->next;
tmp->next = newNode;
newNode->next = tmp2;
return;
}
tmp = tmp->next;
}
}
void print()
{
if(isEmpty()) return;
cout << "list = ";
Node* tmp = head;
while(tmp)
{
cout << tmp->val << " ";
if(tmp == tail) break;
tmp = tmp->next;
}
cout << endl;
}
};
int main()
{
cout << "delete added" << endl;
List testList;
testList.pushBack(5);
testList.pushBack(10);
testList.pushBack(15);
testList.pushBack(20); // after this line,we got segmentation fault
cout << "len = " << testList.findLen() << endl;
testList.print();
testList.pushFront(5);
testList.pushFront(10);
testList.pushFront(15);
testList.pushFront(20);
cout << "len = " << testList.findLen() << endl;
testList.print();
testList.inserter(0,8);
cout << "len = " << testList.findLen() << endl;
testList.print();
testList.inserter(9,555);
cout << "len = " << testList.findLen() << endl;
testList.print();
testList.inserter(5,333);
cout << "len = " << testList.findLen() << endl;
testList.print();
cout << "popBack" << endl;
testList.popBack();
cout << "len = " << testList.findLen() << endl;
testList.print();
cout << "popFront" << endl;
testList.popFront();
cout << "len = " << testList.findLen() << endl;
testList.print();
cout << "popBack" << endl;
testList.popBack();
cout << "len = " << testList.findLen() << endl;
testList.print();
cout << "popFront" << endl;
testList.popFront();
cout << "len = " << testList.findLen() << endl;
testList.print();
return 0;
}
跟进:嘿,伙计们,我自己有一些线索。我认为问题应该出在操作系统方面。在检查了相关的汇编代码之后,我注意到,即使局部变量的默认初始化值为0,它们也不总是零。我认为问题应该出在操作系统的分页方案上。我将尽力弄清楚MacOS(内核10.15.1)和linux如何选择页面以及如何为局部变量生成随机值。如果有人知道这一领域或有任何线索可以解决,请随时告诉我。干杯。