调试:运行时检查失败#2-变量'LoggerThread'周围的堆栈已损坏

时间:2019-07-11 20:04:26

标签: c++

调试:运行时检查失败#2-变量'LoggerThread'周围的堆栈已损坏。

我找不到解决方案:(反正可以解决这个问题。

#include <iostream>

using namespace std;

int main()
{
    string LoginMessage = "Please Login";
    string loginsuccess = "Login Correctly";
    float x = 0;
    char afpl[6][7] = {

        "A",
        "B",
        "C",
        "@",
        "*$",
        "$"
    };

    cout << LoginMessage << endl;

    float y = 10;
    cin >> x;

    int i = /* Would be random - */ 20;

    int finalx = x + y * i;
    int temp = finalx;
    int last = temp / i;
    if (last > 5) {
        last = last / 3;
    }

    cout << afpl[last] << "|" << last << "|" << finalx << endl;
    if (finalx == 210) {
        if (last == 3) {
            if (afpl[6][7] = '@') {
                cout << loginsuccess << endl;
            }
        }
    }

}

没有警告,只有ERROR,这是输出

'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Users\MYNAME\source\repos\PythonVsCSVsC++\Debug\PythonVsCSVsC++.exe'. Symbols loaded.
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. 
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. 
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. 
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. 
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. 
'PythonVsCSVsC++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 
The thread 0x188c has exited with code 0 (0x0).
Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted.

我该如何解决此问题,我尝试在各处找到解决方法,但所有解决方案都无法解决我的问题。无论如何,有没有解决这个问题的方法:),顺便说一下,这是c ++。

1 个答案:

答案 0 :(得分:1)

if (afpl[6][7] = '@')分配给afpl。我认为您的意思是if (afpl[6][7] == '@')。另外afpl[6][7]超出范围。那是未定义的行为,是什么导致堆栈损坏(写到错误的内存位置)。数组索引从0开始。