首页 > 解决方案 > 分段错误:无法修复问题

问题描述

我是 C++ 新手,我正在尝试编写一个通过命令行交互的项目。现在,每当我运行我的主程序(它是可执行文件)时,我总是在主程序完成时收到分段错误错误。


编辑评论:
导师告诉我尽量少使用 C++ 特性,如向量或字符串……我对 C++ 也很陌生,所以我试图尽可能多地使用基本的 C 函数。


我是我的主要功能如下所示:

int main(int argc, char** argv) {
    cout << "starting mvote..." << endl;
    int run_flag = 1;
    char* actionBuffer = (char*)malloc(100 * sizeof(char));
    char* action = (char*)malloc(16 * sizeof(char));
    char* readPtr;
    char exit[4] = { 'e','x','i','t' };

    //parse command line argumentand get the filename
    char* filename = argv[2];
    cout << filename;
    FILE* fp;
    char line[64];

    //from here, I'm opening the file and read it by lines
    fp = fopen(filename, "r");
    if (fp == NULL) {
        cout << "file not exists";
        return -1;
    }

    while (fgets(line, 64, fp) != NULL) {
        cout << line << "\n";
    }
    fclose(fp);


    while (run_flag == 1) {


        cout << "what do you want?\n " << endl;
        cin.getline(actionBuffer, 1024);

        if (strcmp(actionBuffer, exit) == 0) {
            cout << "bye!";

            run_flag = 0;
            break;
        }
        //if not exit, Look for the space in the input
        readPtr = strchr(actionBuffer, ' ');


        int size = readPtr - actionBuffer;
        //extract the operation

        strncpy(action, actionBuffer, size);
        for (int i = 0; i < size; i++) {
            cout << "operation:" << action[i];
        }


        // depend on the operation specified before the first empty space

        run_flag = 0;
    }
    free(actionBuffer);
    free(action);
    return 0;


} 

说明

我首先尝试打开一个与 main 位于同一文件夹中的 csv 文件,然后逐行读取该文件。然后,我只实现了一个简单的命令,您可以在其中键入 exit 并退出程序。
我分配了两个内存actionBufferaction,用于保存命令


问题:当我输入exit并按Enter键时,总是存在分段错误[core dumped],然后进程完成。


研究:所以​​我了解到分段错误是由于访问了不属于我的内存。但是在我的程序中,我试图访问这样的内存吗?


任何建议表示赞赏!谢谢你。

标签: c++segmentation-fault

解决方案


只是给你一个想法,这将是 C++ 代码的一个例子

#include<iostream>
#include<fstream>
#include<string_view>
#include<string>
#include<sstream>
#include<exception>

int main(int argc, char** argv) {
    std::cout << "starting mvote...\n";

    //parse command line argumentand get the filename
    std::string filename = argv[2]; // NO CHECKS!
    std::cout << filename <<'\n';

    //from here, I'm opening the file and read it by lines
    {
        std::ifstream ifs(filename);
        if (!ifs) {
            throw std::invalid_argument("file not exists");
        }
        std::string line;
        while (std::getline(ifs, line)) {
            std::cout << line << '\n';
        }
    }

    bool run_flag = true;
    while (run_flag) {
        std::cout << "what do you want?\n";
        std::string userInput;
        std::getline(std::cin, userInput);
        if (userInput == "exit") {
            std::cout << "bye!\n";
            return 0;
        }
        std::stringstream userInputSs(userInput);
        std::string operation;
        while(userInputSs >> operation){
            std::cout << "operation: " << operation << '\n';
        }
    }
}

推荐阅读