Unhandled exception at 0x00363A09, Stack cookie instrumentation code detected a stack-based buffer overrun

1

So honestly I can't tell you whats wrong with this. I've read threads with similar problems but people are dealing with allocation of memory and things and that is waaaaaayyyy above my scope as a programmer so far, and my program does nothing nearly so complicated.

int main() {
double input[5] = { 5.0, 6.0, 8.0, 4.3, 5.6 };
GradeBook test(sizeof(input), input);


test.bubbleSort();
test.printAll();



return 0;
};

These are my private data members

const static int gradeBookSize = 6;
int classSize;
double grades[gradeBookSize];
bool insertionSorted = false; //simply for efficency
bool bubbleSorted = false;

Constructor for my GradeBook Class

GradeBook(int inputSize, double inputGrades[]) {
    classSize = inputSize;
    for (int i = 0; i < classSize; i++) {
        grades[i] = (inputGrades[i]);
    }
    for (int i = classSize; i < sizeof(grades); i++) {
        grades[i] = 0;
    }

}

And finally the two methods Ive actually used in my main() method

void bubbleSort() {
    //sorts grades in descending order using bubblesort algorithm
    bool sorted = false;
    while (!sorted) {
        for (int i = 0; i < (sizeof(grades) - 1); i++) {
            if (grades[i] < grades[i + 1]) {
                double tmp = grades[i + 1];
                grades[i + 1] = grades[i];
                grades[i] = tmp;
            }
        }
        bool test = false;
        for (int i = 0; i < sizeof(grades) - 1; i++) {
            if (grades[i] < grades[i + 1]) test = true;
        }
        sorted = !test;
    }


    bubbleSorted = true;
    insertionSorted = false;
}

void printAll() {
    for (int i = 0; i < sizeof(grades); i++) {
        cout << grades[i] << "\t";
    }
    cout << endl;
}

And here we have my debug output, I cannot make heads or tails of this

The thread 0x3378 has exited with code 0 (0x0).
Unhandled exception at 0x0130FC38 in CS260_Project4_James_Casimir.exe:0xC00001A5: An invalid exception handler routine has been detected (parameters: 0x00000003).

CS260_Project4_James_Casimir.exe has triggered a breakpoint.

Run-Time Check Failure #2 - Stack around the variable 'test' was corrupted.

Unhandled exception at 0x00363A09 in CS260_Project4_James_Casimir.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

Unhandled exception at 0x00363A09 in CS260_Project4_James_Casimir.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

Unhandled exception at 0x00363A09 in CS260_Project4_James_Casimir.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

The program '[7400] CS260_Project4_James_Casimir.exe' has exited with code 0 (0x0).
c++
asked on Stack Overflow May 1, 2016 by mhm.sherpa

1 Answer

5

The main problem is that you're using sizeof incorrectly. The sizeof keyword returns the number of bytes the type consists of, not the number of items in the array.

What winds up happening in your code is that instead of the number of items in the array, you're actually using (in the case of the grades array)

sizeof(double)*6

which will more than likely be 48 (6 items in the array, each item is 8 bytes). The problem obviously is that your loops will iterate too many times, causing an out-of-bounds memory access in your array accesses (and as you can see, a crash).

If the type is an array, then the number of items in the grades array using sizeof would be:

sizeof(grades) / sizeof(grades[0])

So it's the number of bytes divided by the number of bytes one entry takes up.


If you want something more intuitive than the above, then using std::array would give this to you:

#include <array>
#include <iostream>

std::array<double, 6> grades;
int main()
{
    std::cout << grades.size();  // will output 6.
}
answered on Stack Overflow May 1, 2016 by PaulMcKenzie • edited May 1, 2016 by PaulMcKenzie

User contributions licensed under CC BY-SA 3.0