C++ 运行时检查失败 #2 - 变量“foo”周围的堆栈已损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25516740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:20:08  来源:igfitidea点击:

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

c++

提问by arsis-dev

I'm studying for an exam and this is on my practice test. The question is "Which type of error does the following code fragment cause?"

我正在为考试而学习,这是我的练习测试。问题是“以下代码片段会导致哪种类型的错误?”

I was pretty sure there would be no errors, but I also can't get it to compile in VS13, I get the error:

我很确定不会有错误,但我也无法在 VS13 中编译它,我收到错误消息:

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

运行时检查失败 #2 - 变量“foo”周围的堆栈已损坏。

    const int MAX = 500;
    int main(void)
    {
        int foo[MAX];
        for (int i = 0; i <= MAX; i++)
        {
            foo[i] = i * 2;
            cout << foo[i] << endl;
        }

    cout << "Press any key to exit." << endl;
    cin.ignore(2);

    return 0;
    }

回答by Mike Seymour

Valid indexes for fooare from 0to MAX-1inclusive. MAXis past the end of the array.

的有效索引为foofrom0MAX-1inclusive。MAX已超过数组的末尾。

Your loop runs up to, and including, MAX. This writes beyond the end of the array, corrupting the stack.

您的循环运行到并包括MAX. 这会写入超出数组末尾的内容,从而破坏堆栈。

Either increase the array size to MAX+1so that MAXis in range; or change the loop condition to i < MAXto stop before reaching MAX.

要么增加数组的大小至MAX+1使得MAX在范围内; 或将循环条件更改i < MAX为在到达之前停止MAX

回答by RISHABH DUBEY

This problem is caused when you try to write too much data to a particular memory address. Typical causes are writing more to a string buffer than you have room for.

当您尝试向特定内存地址写入过多数据时会导致此问题。典型的原因是向字符串缓冲区写入的内容超出了您的空间。

ie

IE

void myfun()

{

    char mybuf[10];



    strcpy(mybuf, "This is definitely more than 10 characters long, it will also cause a Run-Time Check");

}

Another cause of this is when you are using memset/ZeroMemory to initialise a structure or array with the wrong size.

另一个原因是当您使用 memset/ZeroMemory 初始化大小错误的结构或数组时。

struct MyStruct

{

    int var;

};



void myfun2()

{

    MyStruct ms;



    ZeroMemory(&ms, 20); //since MyStruct is only one variable in the struct this will cause problems

}

A third possible problem is if you are accidentaly moving a pointer.

第三个可能的问题是您是否不小心移动了指针。

void myfun3()

{

    int a;

    int*b = &a;



    a++;

    *a = 20;

}

Of course, these problems are not so easy as above to find out, but I hope this will get you on the right track. But this happens and will break at the end of the function that the stack is corrupted in while it is returning. So the best place to look would be in the function that your LoggerThread variable is in.

当然,这些问题并不像上面那么容易找出来,但我希望这能让你走上正轨。但是这种情况会发生,并且会在堆栈返回时堆栈损坏的函数的末尾中断。所以最好看的地方是你的 LoggerThread 变量所在的函数。

回答by Aaron

It's usually allocating an array member past the max. I made a little password cracker with a large source array and no probs. I found I was calling an extra cycle past the max array size due to a loop condition mistake.

它通常分配超过最大值的数组成员。我用一个大的源数组制作了一个小密码破解器,没有任何问题。我发现由于循环条件错误,我正在调用超过最大数组大小的额外循环。