【问题标题】:Segmentation fault caused by simple c++ code简单c++代码导致的分段错误
【发布时间】:2019-09-09 16:18:07
【问题描述】:

这里绝对是初学者。我正在尝试解决 this 但遇到分段错误。我尝试寻找解决方案,但找不到不起作用的原因。

要重现错误,只需复制下面的代码并将其粘贴到上面链接下的编辑器中。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
//using namespace std;


int main() {
    int n, q;
    std::cin >> n >> q;

    std::vector<std::vector<int>> arr;

    // handle n lines representing the arrays
    for (int i = 0; i < n; i++) {
        std::vector<int> numbers;
        std::string line;
        getline(std::cin, line);
        std::istringstream iss(line);
        int enterNumber;
        while (iss >> enterNumber)
        {
            numbers.push_back(enterNumber);
        }

        arr.push_back(numbers);
    }

    // handle q lines representing i, j
    int x, y;
    for (int i = 0; i < q; i++) {
        std::cin >> x >> y;
        std::cout << arr[x][y];
    }

    return 0;
}

我错过了什么?为什么它不起作用?

导致分段错误的输入:

2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

预期输出:

5
9

【问题讨论】:

  • 您是否在编译时打开了所有可能的警告,然后在尝试编译然后运行之前确保没有警告?
  • 不保证xy 存在。也许添加支票会有所帮助?
  • 您能否粘贴遇到分段错误时使用的输入??
  • 此时,您应该使用调试器逐行单步执行代码,并找出程序第一次发生意外的时间。不管hackerrank是否提供了一个,你都需要一个调试器来有效地编程。
  • 是时候调试代码了!

标签: c++ segmentation-fault sanity-check


【解决方案1】:

1) getline(std::cin, line); 。行包含空格和数字。

2) 处理行中的空格

3) 处理数组长度的第一个 int。 (您试图在数组本身中添加数组长度)

这是供参考的工作代码。 (通过所有测试用例)

#include <vector>
#include <iostream>
using namespace std;
int main() {
    int n, q;
    cin >> n >> q;
    vector< vector<int> > arr;

    int temp, array_count;
    for(int i = 0; i < n; i++) {
        vector<int> numbers;
        cin>>array_count;
        for(int j = 0; j < array_count; j++) {
            cin>>temp;
            numbers.push_back(temp);
        }
        arr.push_back(numbers);
        numbers.clear();
    }

    // handle q lines representing i, j
    int x, y;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        cout << arr[x][y]<<"\n";
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    要修复分段错误,只需在第一个 std::cin(主函数中的第三行)之后添加 std::cin.get();

    发生分段错误是因为getline(std::cin, line);在 for 循环的第一次迭代期间返回了一个空字符串。见this

    请注意,即使修复了分段错误问题,您的代码仍然是错误的(不能解决挑战):p

    试试这个:

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    //using namespace std;
    
    
    int main() {
        int n, q;
        std::cin >> n >> q;
        std::cin.get();
    
        std::vector<std::vector<int>> arr;
    
        // handle n lines representing the arrays
        for (int i = 0; i < n; i++) {
            std::vector<int> numbers;
            std::string line;
            getline(std::cin, line);
            std::istringstream iss(line);
            int enterNumber;
            while (iss >> enterNumber)
            {
                numbers.push_back(enterNumber);
            }
    
            numbers.erase(numbers.begin());  // because first value is k
            arr.push_back(numbers);
        }
    
        // handle q lines representing i, j
        int x, y;
        for (int i = 0; i < q; i++) {
            std::cin >> x >> y;
            std::cout << arr[x][y] << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多