C 链接列表仅在GNU/Linux而非Windows中导致分割故障
C++ linked list causing segmentation fault only in GNU/Linux not Windows
我从正在进行的练习中有一个代码段。它读取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
遍历列表,直到work
为NULL
,但是work
从列表节点的next
字段中获取其值,而该字段从未初始化。到达结尾时,最后一个节点在next
字段中包含垃圾,您的程序死亡。
为什么在Windows上而不是Linux上可以的是不确定行为的工件,这是您尝试访问非初始化变量时所获得的。
相关文章:
- 分段故障(堆芯转储)矢量
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++中的动态铸造故障
- 数组的指针从不分段故障
- 在Linux for Windows上编译C++代码时出错
- 在Windows上用C++裁剪HBITMAP
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- vscode g++链路故障:体系结构x86_64的未定义符号
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 从 dll 调用 opencv 垫到 Windows 表单,图像出现故障
- strcat 3 char从char []到char *在Windows上工作,但在Cygwin中给出了细分故障
- 功能本地静态为Windows XP生成故障代码
- 在Windows和Linux上使用Boost/ASIO在MingW中使用Boost/ASIO进行故障
- C 链接列表仅在GNU/Linux而非Windows中导致分割故障
- 从任务管理器获取应用程序故障转储中显示的状态在 Windows 上是否准确
- 调用GetSystemTime()函数时出现分段故障(Windows)
- QtSerialPort有不可用的数据,分割故障仅在Windows 8下
- Microsoft Windows UVC扩展单元示例编译故障排除