【问题标题】:Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted运行时检查失败 #2 - 变量“x”周围的堆栈已损坏
【发布时间】:2010-09-21 02:12:25
【问题描述】:

我在以下代码中返回时收到此运行时检查失败。我相信类似的代码在程序的其他地方运行良好。有什么想法吗?

String GetVariableName(CString symbol, CString filepath)
{
    char acLine[512];
    char acPreviousLine[512];
    CString csFile;
    FILE *fp;   

    csFile.Format("%svariables.txt", filepath);

    fp = fopen(csFile, "r");

    if (! fp)
        return("");

    for (;;)
    {
        strcpy(acPreviousLine, acLine);

        // NULL means we are out of lines in the file.
        if (myfgets(acLine, 511, fp) == NULL)
            break;

        // "END" indicates end of file
        if (! strcmp(acLine, "END"))
            break;

        if (! strcmp(acLine, csVarSymbol))
        {
            // Previous line should be variable name
            fclose(fp);

            // Following line results in Check Failure while in Debug mode
            return(acPreviousLine);
        }
    }   
    fclose(fp);
    return("");
}

【问题讨论】:

  • 在示例中围绕哪个变量堆叠?

标签: c++ stack


【解决方案1】:

以上示例中没有变量“x”,但我假设您编辑了错误消息!

acLine 没有初始化,所以当你第一次将它复制到 acPreviousLine 时,你正在复制堆栈中发生的任何内容。这可能会给您带来缓冲区溢出,因此在某些情况下会导致堆栈损坏 - 并非全部,因为您可能很幸运并且在达到 512 字节之前在 acLine 中找到了一个空值。

堆栈在返回时被检查是否损坏,因为在所有堆栈变量周围插入了警戒词(在这个平台和构建配置 - 我假设在 Windows 上,在 VS 上以调试模式编译)以检查该问题.

将 acLine[0] 初始化为 0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2013-12-10
    • 2015-02-09
    • 2015-05-24
    • 2021-12-31
    • 2014-10-20
    • 2014-07-01
    相关资源
    最近更新 更多