调试C 代码时,访问违规错误

Access violation error thrown while debugging C++ code

本文关键字:错误 访问 代码 调试      更新时间:2023-10-16

我正在尝试构建一个解析器,并将值存储在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,但它应该是第一次完成工作。

通常,如果您不想使用标准容器来解决问题,您仍然可以尝试编写内部使用您定义的结构的类。这样,您可以隐藏不必要的实现详细信息:内部存储头部,正确处理清理过程(目前尚未完成)等。