Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:22:38+00:00 2026-06-13T09:22:38+00:00

I have a code snippet below from an exercise I’m working on. It reads

  • 0

I have a code snippet below from an exercise I’m working on. It reads a CSV and enters it into a linked list then prints to the console. The CSV looks like this:

5,3,19
7,12,2
13,15,25
22,0,7

It compiles using both Visual Studio 2010 and G++ in both Linux and Windows. The binary executes in the Windows XP command prompt but a segmentation fault occurs when run in Git Bash (Windows XP) and under Linux. Using a debugger (under Linux) I’ve isolated the problem to printList() not recognising the end of the linked list.

Why is this occurring and what can I do to prevent it? Any suggestions would be greatly appreciated.

#include <cstdlib>
#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;

// CSV source file parameters
const char *cSourceCSV = "source.csv";
const int iFieldsPerRow = 3;

enum direction_t {UP=1, STATIONARY=0, DOWN=-1};

// struct to hold data in a singly linked list
struct CallList {
  float fTime; // time of call in seconds from beginning
  int iFromPos;
  int iToPos;
  direction_t d_tDirectionWanted();
  CallList *next;
};

direction_t CallList::d_tDirectionWanted() {
  int iBalance = iFromPos - iToPos;
  direction_t d_tDirection;
  if (iBalance < 0) d_tDirection = DOWN;
  else if (iBalance == 0) d_tDirection = STATIONARY;
  else if (iBalance > 0) d_tDirection = UP; 
  return d_tDirection;
}

CallList *head;
CallList *temp;
CallList *work;

void populateList(const char *cSourceCSV) {
  string sRow;
  string sValue;
  ifstream ioSource (cSourceCSV); // the source file placed in an input stream 
  if (ioSource.is_open()) { // making sure the stream/file is open
while (ioSource.good()) { // repeat while stream is still healthy
  // obtain the data
  temp = new CallList;
  getline (ioSource,sRow); // reading each row of data
  stringstream s_sRow(sRow); // now entering the row into a stringstream
  for (int i=0; i<iFieldsPerRow; i++) { 
    ws(s_sRow); // if there is whitespace in s_sRow remove it <-this is 
        // stopping the program from crashing but I get an extra line 1,1,1
    getline (s_sRow,sValue,','); // now getting the data from the 
                // stringstream using the comma as a delimiter
    if (i==0) {
      temp->fTime = stof(sValue);
    }
    else if (i==1) { 
      temp->iFromPos = stoi(sValue);
    }
    else if (i==2) {
      temp->iToPos = stoi(sValue);
    }
  }
  // the stationary calls are excluded
  if (temp->d_tDirectionWanted() == STATIONARY) continue;

  // place the remaining data in the linked list
  if (head == NULL) {
    // insert the head
    head = temp;
  }
  else {
//********* THIS WORKS *************
    work = head;
    // nothing fancy needed here as list is already in time order
    while(work != NULL) {
      if (work->next == NULL) {
    work->next = temp;
    break;
      }
      work = work->next;
    }
  }
//************************************
}
ioSource.close();
  }
  else cout << "Error opening file: " << cSourceCSV << endl;
  return;
}

//********* BUT THIS DOESN'T, WHY? *************
void printList(){
  work = head;
  while (work != NULL) {
printf("Time: %*.1f, From: %*i, To: %*i, Dir: %*i\n", 5, work->fTime, 2, work->iFromPos, 2, work->iToPos, 2, work->d_tDirectionWanted());
if (work->next == NULL) break;
else work = work->next;
  }
  return;
}
//************************************


int main(int argc, char *argv[]) {
  populateList(cSourceCSV);
  printList();
  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:22:39+00:00Added an answer on June 13, 2026 at 9:22 am

    When you allocate your CallList node the first time, set the next field to null.

    temp = new CallList;
    temp->next = NULL;
    

    Your printList traverses the list until work is NULL but work gets its value from the next field of your list nodes which are never initialized. When it gets to the end, the last node contains garbage in the next field and your program dies.

    Why this is OK on Windows and not on Linux is an artifact of Undefined Behavior which is what you get when you try to access an uninitialized variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code snippet, which loads data from a CSV file into
Below is a code snippet from the file I am working with. I will
I have code similar to the snippet below (taken from http://leastprivilege.com/2009/05/24/use-geneva-session-management-for-your-own-needs/ ) public class
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I have below snippet of code in which TestClass is extending jPanel which is
I have this snippet of code below. I want to pass the value of
Below is the snippet of my code. I have a JTable. I have extended
i have below code snippet. I am not getting how to change the color
i have below code snippet. It throws the exception at line 3 but query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.