Help with little histogram program

For an assignment I have to make this little thing that displays crude histograms. i'm getting this error, and I can't figure out what's wrong, help would be greatly appreciated.

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;


// read data from console
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];
  }
}

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

    *minDataValue = data[0];
    *maxDataValue = data[0];

    int i = 0;
    while (i < dSize) {
        if (*minDataValue < data[i]){
            *minDataValue = data[i];
        }
        if (*maxDataValue > data[i]){
            *maxDataValue = data[i];
        }
        i++;
    }

    int difference = (*maxDataValue - *minDataValue)+1;

    int *freqArray;
    freqArray = new int[difference];

    int k = 0;
    while (k < difference){
        freqArray[k] = 0;
        k++;
    }

    int j = 0;
    while (j < difference) {
        int temp = data[j] - *minDataValue;
        freqArray[temp] += 1;
        j++;
    }

    return freqArray;
}





// make histogram from the frequency array
void makeHistogram ( int *freq, int min, int max ){

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

  int size = max - min + 1;
  for (int i = 0; i < size; i++){
    if (freq[i] > 0){
      cout << "\n" << min + i << ": ";
      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;
  int min,max;

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

  if (frequency == NULL) return -1;

  makeHistogram (frequency, min, max);

  return 0;
}
Have you tried stepping through the code in a debugger to find the bad memory allocation? Its probably line 34, and it might be that you got your variable allocations the wrong way round, and you are now ending up with attempting to allocate a negative sized block of memory. Also, on line 90, frequency will never equal nullptr (NULL), because new throws on failure unless you specify the nothrow variant.

EDIT:
Yes, you have your comparison operators the wrong way round in lines 22 and 25.
Last edited on
fixing 22 and 25 fixed the issue, thanks! once it was mostly working there were a few more smaller errors that i was able to fix. and now it's done!
Topic archived. No new replies allowed.