通用链表指针访问
我正在使用模板在 C++ 中编写通用链表,并且在访问节点值时遇到分段错误。
为了使测试用例更简单,我实现了一个固定大小的两个节点的链表。
我有两个问题:
1a)为什么 aList.headNodePtr->prevNodePtr 不设置为 NULL?
1b) 为什么 aList.tailNodePtr->nextNodePtr 不设置为 NULL?
我在 LinkedList 构造函数中将这两个值设置为 NULL,但 main 中的输出显示:
head prevAddress: 0x89485ed18949ed31
tail nextAddress: 0x7fffe8849679
2) 为什么 main() 中的以下行会导致段错误?
aList.headNodePtr->nodeValue = 1;
完整代码如下:
#include <iostream>
using namespace std;
template <class T>
class Node {
public:
Node<T>* prevNodePtr;
Node<T>* nextNodePtr;
T nodeValue;
};
template <typename T>
class LinkedList {
public:
Node<T>* headNodePtr;
Node<T>* tailNodePtr;
LinkedList() {
Node<T>* headNodePtr = new Node<T>;
Node<T>* tailNodePtr = new Node<T>;
headNodePtr->prevNodePtr = NULL;
headNodePtr->nextNodePtr = tailNodePtr;
tailNodePtr->prevNodePtr = headNodePtr;
tailNodePtr->nextNodePtr = NULL;
}
~LinkedList() {
headNodePtr = NULL;
tailNodePtr = NULL;
delete headNodePtr;
delete tailNodePtr;
}
};
int main()
{
LinkedList<int> aList;
cout << "head Value: " << aList.headNodePtr->nodeValue << endl;
cout << "head prevAddress: " << aList.headNodePtr->prevNodePtr << endl;
cout << "head nextAddress: " << aList.headNodePtr->nextNodePtr << endl;
cout << "tail Value: " << aList.tailNodePtr->nodeValue << endl;
cout << "tail prevAddress: " << aList.tailNodePtr->prevNodePtr << endl;
cout << "tail nextAddress: " << aList.tailNodePtr->nextNodePtr << endl;
aList.headNodePtr->nodeValue = 1;
}
I am writing a generic linked list in C++ using templates, and am experiencing Segmentation Faults when accessing Node values.
To make the test case simpler, I have implemented a fixed size, two node, linked list.
I have two questions:
1a) Why isn't aList.headNodePtr->prevNodePtr set to NULL?
1b) Why isn't aList.tailNodePtr->nextNodePtr set to NULL?
I set both of these values to NULL in the LinkedList constructor, but the output in main shows that:
head prevAddress: 0x89485ed18949ed31
tail nextAddress: 0x7fffe8849679
2) Why does the following line in main() cause a Seg Fault?
aList.headNodePtr->nodeValue = 1;
The full code is below:
#include <iostream>
using namespace std;
template <class T>
class Node {
public:
Node<T>* prevNodePtr;
Node<T>* nextNodePtr;
T nodeValue;
};
template <typename T>
class LinkedList {
public:
Node<T>* headNodePtr;
Node<T>* tailNodePtr;
LinkedList() {
Node<T>* headNodePtr = new Node<T>;
Node<T>* tailNodePtr = new Node<T>;
headNodePtr->prevNodePtr = NULL;
headNodePtr->nextNodePtr = tailNodePtr;
tailNodePtr->prevNodePtr = headNodePtr;
tailNodePtr->nextNodePtr = NULL;
}
~LinkedList() {
headNodePtr = NULL;
tailNodePtr = NULL;
delete headNodePtr;
delete tailNodePtr;
}
};
int main()
{
LinkedList<int> aList;
cout << "head Value: " << aList.headNodePtr->nodeValue << endl;
cout << "head prevAddress: " << aList.headNodePtr->prevNodePtr << endl;
cout << "head nextAddress: " << aList.headNodePtr->nextNodePtr << endl;
cout << "tail Value: " << aList.tailNodePtr->nodeValue << endl;
cout << "tail prevAddress: " << aList.tailNodePtr->prevNodePtr << endl;
cout << "tail nextAddress: " << aList.tailNodePtr->nextNodePtr << endl;
aList.headNodePtr->nodeValue = 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上并没有设置成员,而是设置您在构造函数中声明的局部变量:
请尝试以下操作:
You're not actually setting the members, you're setting the locals you declared in the ctor:
Try this instead: