找到分段错误

时间:2018-04-23 05:42:01

标签: c++ segmentation-fault

我有以下代码。我阅读了有关分段错误的指南,但我不能100%确定它在我的代码中实际发生的位置。它一直有效,直到我开始使用动态数组(直方图),更具体地说是//将所有初始值设置为零。在我不确定之后的那个烂摊子里。谢谢!

教师要求“使用动态数组存储直方图。”,我认为这是我的问题。

-Solved-

感谢您的帮助,错误在于我如何初始化数组指针

而不是

const int hSize = 10;
IntArrayPtr histogram;
histogram = new int[hSize];

我用过

const int hSize = 10;
int hValues[hSize] = { 0 };
IntArrayPtr histogram;
histogram = hValues;

这是教练想要的。

#include <iostream>
#include <vector>

using namespace std;

typedef int* IntArrayPtr;

int main() {

    vector<int>grades;
    int newGrade;

    cout << "Input grades between 0 and 100. Input -1 to calculate histogram: " << endl;
    cin >> newGrade;
    grades.push_back(newGrade);
    while (newGrade > 0) {
        cin >> newGrade;
        while (newGrade > 100) {
            cout << "less than 100 plz: ";
            cin >> newGrade;
        }
        grades.push_back(newGrade);
    }
    grades.pop_back();
    int size = grades.size();
    cout << "Calculating histogram with " << size << " grades." << endl;

    //Create dynamic array for the histogram of 10 sections.
    const int hSize = 10;
    IntArrayPtr histogram;
    histogram = new int[hSize];


    }
    //Make the historgram
    int stackValue = 0;
    for (int j = 0; j < hSize; j++) {
        //Loop through the grade vector slots
        for (int i = 0; i < size; i++) {
            int testValue = grades[i];
            //If the grade at the index is between the stack values of the histogram add one to the value of the slot
            if (testValue > stackValue && testValue < stackValue + 10) {
                histogram[j]++;
            }
        }
        //After looping through the vector jump up to the next histogram slot and corresponding stack value.
        stackValue += 10;
    }

    //Histogram output. Only output the stacks with values
    for (int i = 0; i < 10; i++) {
        if (histogram[i] != 0) {
            cout << "Number of " << (i + 1) * 10 << "'s: " << histogram[i];
        }
    }

    return 0;
}

工作代码:

#include <iostream>
#include <vector>

using namespace std;

typedef int* IntArrayPtr;

int main() {

    vector<int>grades;
    int newGrade;

    cout << "Input grades between 0 and 100. Input -1 to calculate histogram: " << endl;
    cin >> newGrade;
    grades.push_back(newGrade);
    while (newGrade > 0) {
        cin >> newGrade;
        while (newGrade > 100) {
            cout << "less than 100 plz: ";
            cin >> newGrade;
        }
        grades.push_back(newGrade);
    }
    grades.pop_back();
    int size = grades.size();
    cout << "Calculating histogram with " << size << " grades." << endl;

    //Create dynamic array for the histogram of 10 sections.
    const int hSize = 10;
    int hValues[hSize] = { 0 };
    IntArrayPtr histogram;
    histogram = hValues;

    //Make the historgram
    int stackValue = 0;
    for (int j = 0; j < hSize; j++) {
        //Loop through the grade vector slots
        for (int i = 0; i < size; i++) {
            int testValue = grades[i];
            //If the grade at the index is between the stack values of the histogram add one to the value of the slot
            if (testValue > stackValue && testValue < stackValue + 10) {
                histogram[j]++;
            }
        }
        //After looping through the vector jump up to the next histogram slot and corresponding stack value.
        stackValue += 10;
    }

    //Histogram output. Only output the stacks with values
    for (int i = 0; i < 10; i++) {
        if (histogram[i] != 0) {
            cout << "Number of " << (i + 1) * 10 << "'s: " << histogram[i] << endl;
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

histogram是指针,而不是数组。

虽然

int histogram[hSize] = {0};

会创建一个零初始化数组,

histogram = { 0 };

将任何元素设置为零(它不能,因为histogram指向一个 int,而不是很多) 。

大括号被忽略 - 从C继承的相当混乱的行为 - 它等同于

histogram = 0;

histogram = nullptr;

你想要

int* histogram = new int[hSize]();

圆括号值初始化数组,反过来又是它的元素 值初始化整数将它们设置为零。

(顺便说一句:输入星号的习惯会产生比它解决的问题更多的问题。不要这样做。)

答案 1 :(得分:1)

Seg错误是访问您无法访问的内存区域的问题,因此您需要查看指针的使用。这通常意味着你有一个指针,它有一个你刚刚取消引用的错误值。

在这种情况下,问题是这一行:

    histogram = { 0 };

这并没有像您想象的那样将直方图值设置为零:它将历史记录图指针重置为零。然后你取消引用导致你的SegFault的指针(注意这行甚至不用clang编译,所以你的编译器在这个上没有帮助你。)

将该行更改为:

memset(histogram, 0, hSize);

在这种情况下会对问题进行排序。

更一般地说,为了诊断段错误,我经常使用两种技巧(虽然避免比治疗更好):

  1. 在调试器下运行程序:调试器可能会在故障点停止程序,你可以确切地看到它失败的地方

  2. 在Valgrind或类似程序下运行程序 - 这也会告诉您错误浮出水面的位置,但在更复杂的故障中也可以告诉您它的位置(通常不是同一个地方)。