简体   繁体   English

为什么对于程序的大值而不是小值,程序崩溃?

[英]Why do I get a program crash for large values but not small values for my program?

Why do I get a program crash for large values but not small values for my program? 为什么对于程序的大值而不是小值,程序崩溃? If I input 1-3 the program does what it is supposed to but when I enter a number greater than that the program crashes and/or does not complete? 如果我输入1-3,程序会执行预期的操作,但是当我输入的数字大于该值时,程序崩溃和/或未完成? Is it something to do with a pointer error or the way I've referenced something? 这与指针错误有关还是与我引用某些内容有关? I'm unsure so any help is appreciated. 我不确定,因此可以提供任何帮助。 Thanks! 谢谢!

Code: 码:

#include <iostream>
using namespace std;

void getData (int size, int *Arr){

    cout << "\n\nEnter integer data one line at a time\n" << endl ;

    for (int i=0; i < size; i++){
        cin >> Arr[i];
    }
}

void findMinAndMax(int array[], int size, int *min, int *max) {

    int smallest = array[0];
    int largest = array[0];

    *min = smallest;
    *max = largest;

    for (int i = 1; i < size; i++)
        {
            if (array[i] > *max){
                *max = array[i];
                cout << "Max Value (loop): " << *max << endl;
            }
            if (array[i] < *min){
                *min = array[i];
                cout << "Min Value (loop): " << *max << endl;
            }
        }

    // testing code
    cout << "Min Value: " << *min << endl;
    cout << "Max Value: " << *max << endl;

}

int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {

    cout << "Min Value Pre: " << *minDataValue << endl;// testing code
    cout << "Max Value Pre: " << *maxDataValue << endl;// testing code
    findMinAndMax(data, dSize, minDataValue, maxDataValue);
    cout << "Min Value Post: " << *minDataValue << endl; // testing code
    cout << "Max Value Post: " << *maxDataValue << endl;// testing code


    int fSize = *minDataValue + *maxDataValue;

    cout << "fSize: " << fSize << endl; // testing code

    int *frequency;
    frequency = new int [fSize];

    // if frequency is 0, end
    if (frequency == 0)
        {
            return 0;
        }

    // set all elements to 0 in array frequency
    for (int i = 0; i <= fSize; i++) {
        frequency[i] = 0;
    }


    for (int i = 0; i <= dSize; i++) {
        int j = data[i] - (*minDataValue) + 1;
        frequency[j] = frequency[j] + 1;
    }

    return frequency;
}

void makeHistogram (int *freq, int min, int max ){

    cout << "Frequency Value HISTOGRAM: " << *freq << endl;

    cout << "\n\n\n ----------- Histogram ----------------\n" << endl;

    int size = min + max;
    cout << "Size Value HISTOGRAM: " << size << endl;

    for (int i = 0; i < size; i++){

        if (freq[i] > 0) {

            cout << "\n" << min + i - 1 << ": ";

            for (int j = 0; j < freq[i]; j++) {
                cout << '*';
            }
        }
    }
    cout << endl << endl;
}

int main() {

    int dSize;
    int *ArrayOfInts;

    cout << "How many data values? ";
    cin >> dSize;

    ArrayOfInts = new int [dSize];

    getData(dSize, ArrayOfInts);


    int *frequency, min, max;

    frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);

    if (frequency == 0) return -1;

    cout << "Min Value MAIN: " << min << endl; // testing code
    cout << "Max Value MAIN: " << max << endl; // testing code
    cout << "Frequency Value MAIN: " << *frequency << endl;

    makeHistogram(frequency, min, max);


    delete [] frequency;

    return 0;
}

One place where you have undefined behaviour which can cause crashes: 一种可能导致崩溃的未定义行为的地方:

here you allocate fSize elements: 在这里,您分配fSize元素:

frequency = new int [fSize];

later you iterate it until fSize : 稍后,您将其迭代直到fSize

for (int i = 0; i <= fSize; i++) {

you should change to i < fSize , because there is no fSize element in your array. 您应该更改为i < fSize ,因为数组中没有fSize元素。 And the same problem with i <= dSize later on. 后来, i <= dSize的同样问题。 Should be i < dSize . 应该是i < dSize

btw. 顺便说一句。 I dont see why only large values should cause crashes in your code, maybe this is just UB. 我不明白为什么只有大的值会导致代码崩溃,也许这只是UB。

You're setting fSize incorrectly. 您错误地设置了fSize It should be the difference between the maximum and minimum values, not the sum of them. 它应该是最大值和最小值之差,而不是它们的总和。 Otherwise, if you have negative numbers in your list, the frequency array will be too small. 否则,如果列表中有负数,则frequency数组将太小。 And if absolute value of any of the negative numbers is larger than the highest number, fSize will be negative, which is not valid for the size of an array. 并且,如果任何负数的绝对值大于最大数,则fSize将为负,这对于数组的大小无效。

Then you need to add 1 to include both endpoints. 然后,您需要添加1以包括两个端点。 So it should be: 因此应该是:

int fSize = *maxDataValue - *minDataValue + 1;

Then, as the other answer pointed out, you need to fix your for loops. 然后,正如另一个答案指出的那样,您需要修复for循环。 When the size of an array is N , the array indexes from from 0 to N-1 . 当数组的大小为N ,数组的索引从0N-1 So it should be: 因此应该是:

for (int i = 0; i < fSize; i++) {

using < as the loop test, not <= . 使用<作为循环测试,而不是<= If you try to write outside an array, you invoke undefined behavior, so anything can happen -- if you're lucky you get a crash, but that's not guaranteed. 如果尝试在数组外部进行写操作,则会调用未定义的行为,因此任何事情都可能发生-如果您很幸运会崩溃,但这并不能保证。

You have a similar problem when you assign to frequency : 分配给frequency时,您也会遇到类似的问题:

for (int i = 0; i <= dSize; i++) {
    int j = data[i] - (*minDataValue) + 1;
    frequency[j] = frequency[j] + 1;
}

There's no need to add 1 when subtracting *minDataValue , and doing so will cause you to go outside the array when data[i] is the maximum. 减去*minDataValue时不需要加1 ,并且这样做会在data[i]最大时使您超出数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM