C 链接列表仅在GNU/Linux而非Windows中导致分割故障

C++ linked list causing segmentation fault only in GNU/Linux not Windows

本文关键字:Windows 故障 分割 而非 Linux 列表 链接 GNU      更新时间:2023-10-16

我从正在进行的练习中有一个代码段。它读取CSV并将其输入到链接列表中,然后将其打印到控制台。CSV看起来像这样:

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

它在Linux和Windows中都使用Visual Studio 2010和G 编译。在Windows XP命令提示符中执行二进制文件,但是当在Git Bash(Windows XP)和Linux下运行时,会发生分割故障。使用调试器(在Linux下),我将问题隔离到printList()不识别链接列表的结尾。

为什么会发生这种情况,我该怎么做才能防止这种情况?任何建议都将不胜感激。

#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: %*in", 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;
}

第一次分配呼叫列表时,将next字段设置为null。

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

您的printList遍历列表,直到workNULL,但是work从列表节点的next字段中获取其值,而该字段从未初始化。到达结尾时,最后一个节点在next字段中包含垃圾,您的程序死亡。

为什么在Windows上而不是Linux上可以的是不确定行为的工件,这是您尝试访问非初始化变量时所获得的。