Reputation: 241
I'm currently trying to solve coding challenge for studying for my intro c++ course and I'm just trying to understand the segmentation fault error I'm getting.
I'm writing a given function to insert nodes at the tail of a linked list. The idea is that given an input of integers the first input would be considered part of the end of the list because it's pointer member variable would be nullptr
. Afterwards, all other inputs would have to be appended so that their insertion occurs before that null node.
Here's my first part of the function:
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
if(head == nullptr) //if the list is empty
{
head -> next = new SinglyLinkedListNode(data);
return head;
}
return head;
}
however this part of the function is giving me a segmentation fault error:
head -> next = new SinglyLinkedListNode(data);
These are the class declarations:
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedList() {
this->head = nullptr;
}
};
and Main:
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
SinglyLinkedList* llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
SinglyLinkedListNode* llist_head =
insertNodeAtTail(llist->head, llist_item);
llist->head = llist_head;
}
print_singly_linked_list(llist->head, "\n", fout);
fout << "\n";
free_singly_linked_list(llist->head);
fout.close();
return 0;
}
I was thinking that :
head -> next = new SinglyLinkedListNode(data);
would create a new node with the data argument as it's member data and then the current head node would point to that list.
I would like to know why this segmentation fault is occurring. Also, I cannot change any of the class declaration or constructors since that's how they are given in the challenge.
GDB Trace:
Reading symbols from solution...done.
[New LWP 22279]
Core was generated by `solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000400eea in insertNodeAtTail (head=<optimized out>,
data=<optimized out>) at solution.cc:62
62 head -> next = new SinglyLinkedListNode(data);
#0 0x0000000000400eea in insertNodeAtTail (head=<optimized out>,
data=<optimized out>) at solution.cc:62
#1 0x0000000000400c2e in main () at solution.cc:88
Upvotes: 2
Views: 366
Reputation: 42
Head is null. You cannot access the 'next' field of a null object.
if(head == nullptr) //if the list is empty
{
head = new SinglyLinkedListNode(data);
return head;
}
return insertNodeAtTail(head->next, data)
Upvotes: 0
Reputation: 40060
Because dereferencing a null pointer (head
IS null) is undefined behavior whose consequence can be a segmentation fault:
if(head == nullptr) //if the list is empty
{
head -> next = new SinglyLinkedListNode(data);
Upvotes: 5