I am having an issue with segmentation fault that I haven't been able to fix. I am hoping for some insight as to why it might be happening. This is a linked list implemented for taking strings and allocating each character to a node in the list. The segmentation fault points to the first line in my copy constructor, that creates the new Node. Code included below.
SLLString.cpp
#include "SLLString.h"
#include <iostream>
using namespace std;
SLLString::SLLString(const string &other)
{
Node *newNode = new Node();
^^^ //Segmentation Fault here
head = newNode;
newNode->data = other[0];
newNode->next;
for (int i = 1; i < other.size(); i++)
{
newNode->next = new Node;
newNode->next->data = other[i];
newNode = newNode->next;
counter++;
}
}
SLLString::~SLLString()
{
Node *currNode = head;
cout << "Delete...";
while (currNode != NULL)
{
head = currNode;
currNode = currNode->next;
cout << head->data << " ";
delete head;
}
cout << endl;
}
SLLString::SLLString(const SLLString &other)
{
Node *newNode = head;
Node *newNode2 = other.head;
while (newNode2 != NULL)
{
newNode->data = newNode2->data;
newNode = newNode->next;
newNode2 = newNode2->next;
}
}
SLLString &SLLString::operator=(const SLLString &other)
{
Node *travNode = other.head;
static SLLString result;
string holder;
while (travNode != NULL)
{
holder += travNode->data;
travNode = travNode->next;
}
result = SLLString(holder);
return result;
}
int SLLString::length()
{
int counter = 0;
Node *currNode = head;
if (currNode = NULL)
{
return 0;
}
while (currNode != NULL)
{
counter++;
currNode->next = currNode;
}
return counter;
}
void SLLString::erase(char c)
{
Node *currNode = head;
if (head = NULL)
{
cout << "List is empty.";
}
while (currNode != NULL)
{
if (currNode->data = c)
{
cout << "Removing: " << c << endl;
currNode->next = currNode->next->next;
currNode = currNode->next;
}
}
}
SLLString &SLLString::operator+=(const SLLString &other)
{
SLLString *result;
result = this;
*result += other;
return *result;
}
char &SLLString::operator[](const int n)
{
Node *current = new Node;
static char c;
int compare = n;
for (current = head; current != NULL; current = current->next)
{
if (compare = counter)
{
c = current->data;
}
}
return c;
}
ostream &operator<<(ostream &os, const SLLString strng)
{
Node *newNode;
newNode = strng.head;
if (newNode != NULL)
{
cout << newNode->data;
newNode = newNode->next;
}
while (newNode != NULL)
{
cout << "->" << newNode->data;
newNode = newNode->next;
}
return os;
}
SLLString.h
#include "node.h"
class SLLString
{
public:
SLLString() { head = NULL; }; // fixed by initializing private variable --REMOVE THIS
SLLString(const string &other);
~SLLString(); //done
SLLString(const SLLString &other); //done
SLLString &operator=(const SLLString &other);
int length();
SLLString &operator+=(const SLLString &other);
char &operator[](const int n);
int findSubstring(const SLLString &substring);
void erase(char c);
Node *head = NULL;
int counter = 0;
friend ostream &operator<<(ostream &os, const SLLString strng);
};
Main
#include <iostream>
#include <string>
#include "SLLString.h"
using namespace std;
int main(){
SLLString str("Hello world!");
SLLString newStr;
newStr = str;
newStr += SLLString(" CS@BC");
newStr[6] = 'W';
cout << newStr;
cout << newStr << endl; // Hello World! CS@BC
cout << newStr.length() << endl; //18
int loc = newStr.findSubstring("World");
cout << loc << endl; // 6
newStr.erase('l'); //erase the letter l.
cout << newStr << endl; // Heo Word! CS@BC
newStr.erase('C');
cout << newStr << endl; // Heo Word! S@B
return 0;
}
Node Class
#include <iostream>
#include <cstddef>
using namespace std;
class Node{
public:
char data = 0;
Node *next = NULL;
};
SLLString
's copy assignment operator has several problems. The one you're seeing is from the result = SLLString(holder);
line, which will recursively call the copy assignment operator, eventually running out of stack space.
Other problems stem from the use of result
(you should be modifying the this
object, not a new one).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.