简体   繁体   English

0x00363A09处未处理的异常,堆栈cookie工具代码检测到基于堆栈的缓冲区溢出

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

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类的构造函数

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 最后是我在main()方法中实际使用的两种方法

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).

The main problem is that you're using sizeof incorrectly. 主要问题是您使用的sizeof错误。 The sizeof keyword returns the number of bytes the type consists of, not the number of items in the array. sizeof关键字返回类型组成的字节数,而不是数组中的项目数。

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) 代码中发生的事情是,您实际使用的是数组而不是数组中的项目数(对于grades数组)

sizeof(double)*6

which will more than likely be 48 (6 items in the array, each item is 8 bytes). 很有可能是48个(数组中有6个项目,每个项目为8个字节)。 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: 如果类型是数组,则使用sizeofgrades数组中的项目数为:

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: 如果您想要比以上更直观的方法,那么使用std::array可以为您提供:

#include <array>
#include <iostream>

std::array<double, 6> grades;
int main()
{
    std::cout << grades.size();  // will output 6.
}

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

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