调试C 代码时,访问违规错误
Access violation error thrown while debugging C++ code
我正在尝试构建一个解析器,并将值存储在C 中的链接列表中。我收到以下错误无法解决的错误,我查看了其他堆栈溢出链接,但找不到正确的答案。
我收到以下错误:
例外,在parse_and_store.exe中在0x000000013FCF3954上投掷:0xc0000005:访问违规位置0x000000000000000018。
如果有此例外的处理程序,则可以安全地继续该程序。
我的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: %dn", buffer, strlen(buffer));
printf("%snn", 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;
}
my dinialial_pair.txt&quot&quot文件包含以下数据:
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
您可以尝试在代码中分析n
的使用。在main
的开始中,您有node *n = NULL;
-罚款。
然后,尽管每行的循环均可将元素拆分,然后尝试将其存储到列表中。查看下面的评论行:
while (std::getline(file, str))
{
// Process str
cout << str << endl;
n = new node; // 1
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]; //2
n = n->next; //3
}
}
对于每条新行,从文件(1)
读取您,您将新值分配给n
。可能这不是您想要的。
在(2)
中,您将读取元素从行分配到数据字段,该元素看起来不错,但在(3)
替换n
中,n->next
指向的内容可能包含垃圾。
之后,您可能想使用printList(n);
打印整个列表,但是由于n
对每行有一个新值,并且可能指出了您的期望,因此代码失败了。
如果您想要的是文件中的所有元素的单个列表,则应将n
视为列表的 head 元素。另外,添加一个附加变量以指向当前列表元素(因此您不会丢失列表root元素)。在内部for
循环内部,您应该为每个条目创建节点元素
for(...){
//....
node* entry = new node;
entry->data = sep[i];
current->next = entry;
current = entry;
}
注意,您应该确保创建第一个node
时同时同时使用n
(被视为头部/root)和current
指向相同的数据。
因此,例如在while
循环之前,您可以放置:
node* current = new node;
node* n = current;
但是,这样,第一个列表条目将不包含任何数据,它将仅用于指向该数据实际启动的位置 - 应该在打印功能中处理。另一方面,您可以跳过以上并在for
环内进行检查,例如:
node* entry = new node;
if (!n)
n = current = entry;
// ...
这不是很漂亮,也不优雅,因为每次循环迭代都会评估该条件,并且仅是true
,但它应该是第一次完成工作。
通常,如果您不想使用标准容器来解决问题,您仍然可以尝试编写内部使用您定义的结构的类。这样,您可以隐藏不必要的实现详细信息:内部存储头部,正确处理清理过程(目前尚未完成)等。
- 尝试从C++访问 UWP 的电子邮件邮件类会导致"REGDB_E_CLASSNOTREG类未注册"错误
- C++ - 循环访问指针数组会导致错误
- 执行错误访问 遍历向量
- 错误访问 读入对象后
- 如何修复 CopyFile() 错误 5 - 访问被拒绝错误
- CreateFile 在尝试打开目录进行读取时始终返回错误 5(访问被拒绝)
- 范围检查检测到的检测代码和超出范围的错误访问
- 查找() 上的 C++ 错误访问错误
- 错误访问另一个插件中的类(错误:未定义的引用)
- C STD ::排序Intel编译器错误:访问违规
- libcurl、OSX、curl_multi_执行错误访问
- C++数组错误:访问冲突读取位置0xC0000005
- C++LinkedList错误:访问冲突错误
- 使用.open()成员函数C++的ifstream数组的错误访问代码
- C++ 段错误 访问对象指针向量的 std::vector.size()
- JNI 从 C 错误访问对象数组
- 带有内存错误访问的C++结构new
- XCode C++上的错误访问错误
- 错误访问冲突写入位置0x00229C20.尝试在控制台中输入字符串时
- MySQL 连接器/C++ 错误访问崩溃