【发布时间】:2016-12-07 09:13:02
【问题描述】:
我正在尝试构建解析器并将值存储在 C++ 中的链表中。我收到以下我无法解决的错误,我查看了其他 Stack Overflow 链接,但找不到正确的答案。
我收到以下错误:
在 parse_and_store.exe 中的 0x000000013FCF3954 处引发异常:0xC0000005:访问冲突读取位置 0x0000000000000018。
如果有这个异常的处理程序,程序可以安全地继续。
我的 parses_and_store.cpp 代码如下:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <direct.h>
// I don't recommend using the std namespace in production code.
// For ease of reading here.
using namespace std;
struct node {
string data;
node *next;
};
// You could also take an existing vector as a parameter.
vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
void printList(node* head)
{
node *tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
cout << tmp->data << endl;
}
}
int main() {
string myCSV = "one two three four";
char *buffer = NULL;
node *n = NULL;
// Get the current working directory:
if ((buffer = _getcwd(NULL, 0)) == NULL)
perror("_getcwd error");
else
{
//printf("%s \nLength: %d\n", buffer, strlen(buffer));
printf("%s\n\n", buffer);
free(buffer);
}
std::ifstream file("differential_pair.txt");
std::string str;
while (std::getline(file, str))
{
// Process str
cout << str << endl;
n = new node;
vector<string> sep = split(str, ' ');
for (int i = 0; i < sep.size(); i++)
{
cout << sep[i] << endl;
//std::string str(sep[i].begin(), sep[i].end());
cout << sep.size() << endl;
std::cin.get();
n->data = sep[i];
n = n->next;
}
}
printList(n);
// If using C++11 (which I recommend)
/* for(string t : sep)
* cout << t << endl;
*/
file.close();
std::cout << "\nText verified!" << std::endl;
std::cin.get();
return 0;
}
我的“differential_pair.txt”文件包含以下数据:
VCC 7 0 12
VEE 8 0 -12
VIN 1 0 AC 1
RS1 1 2 1K
RS2 6 0 1K
Q1 3 2 4 MOD1
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
好的,我会用调试器检查。
-
调试器只是将我直接带到“xstring”文件,它会将我引导到以下语句: if (this->_Myres() _Mysize( )); // 重新分配以增长
-
请注意,请求紧迫性就像在问题上画一个大的反对票目标 - 不要这样做!对于志愿者来说,这从不紧急。
-
看起来你正在取消引用一个未初始化的指针。
标签: c++ arrays vector linked-list structure