Can anyone help me figure out why I have a segmentation fault error occurring during runtime? I'm guessing that I am accessing memory that doesn't exist or attempting to place data into a node that hasn't been allocated, but can not figure it out.

FYI - It's a work in progress, haven't written to the outfile yet, cleaned up unnecessary variables etc - but this segmentation fault issue is my biggest nemisis. The Node class constructor just initializes a string data variable and a null pointer.

#include <iostream>
#include <iomanip>
#include <fstream>  // provides file handling
#include <cassert>  // provides file assertions
#include <string>  // provides string manipulation
#include "node1.h"  // provides node class
using namespace std;
using namespace my_linked_list;


int main ()
{
   int i = 0;
   string revfile;
   string tempstr;
   string readystr;
   string c;
   node *begin_ptr;
   node *cursor;
   ifstream infile;   
   ofstream outfile;  

   cout << "What is the name of the text file you want to see in reverse order?  ";
   cin >> revfile;
   cout << endl;

   infile.open(revfile.c_str());
   assert(infile);           

   while (infile) {
        while (getline(infile, tempstr)) {
           for (i=0; i<tempstr.length() ; i++){
                if (tempstr[i] == ' ' && tempstr[i+1] == ' ')   {
                   infile.ignore( );
                }
                else if (tempstr[i] == ' ') {
                   c = tempstr[i];
                   readystr = readystr + c;
                   begin_ptr = new node(readystr,begin_ptr);
 }
                else {
                   c = tempstr[i];
                   readystr = readystr + c;
                }
           }
        if (readystr.length() > 0){
           begin_ptr = new node(readystr,begin_ptr);
           readystr.erase(0, readystr.length());
        }
        begin_ptr = new node("add_a_newline_in_output",begin_ptr);
        }
   }
   infile.close( );
   outfile.open("output.txt");
   cout << "did we make it here?";
   for (cursor = begin_ptr ; cursor != NULL ; cursor = cursor->link())
           cout << cursor->data();
   return 0;
}
for (i=0; i<tempstr.length() ; i++){
  if (tempstr[i] == ' ' && tempstr[i+1] == ' ')   {

Never use i+1 as an array index unless you account for it in your loop control. When your for loop gets to the last element of the array, you go past its boundaries with the i+1 part of the statement. This is the very nature of a seg fault. When you do that, you are accessing memory that isn't assigned to that array and most likely not assigned to the program either.

You are better off either starting i at 1 and using an (i - 1) statement or ending your loop at (tempstr.length() - 1)

Edited 6 Years Ago by Fbody: n/a

Thanks for the help. I fixed the first issue you mentioned but am still getting a segmentation fault. If I comment out the FOR loop that prints all the data, then the segmentation fault does not occur, but then i'm not printing my output either...

for (cursor = head_ptr ; cursor != NULL ; cursor = cursor->link())
           cout << cursor->data();

Could it be that I am not properly setting any of the node links to NULL so therefore cursor is never equal to NULL? Appreciate the help!

OK - I just fixed it. I was not setting the head_ptr = NULL to begin with because I thought the constructor was doing it by setting the node->next initial value to NULL.

Thanks for the guidance! I'll not use [i+1] in FOR loops until i know what the heck i'm doing.

This question has already been answered. Start a new discussion instead.