繁体   English   中英

VC ++ 6.0中的调试与发布行为之谜

[英]Debug vs Release behavior mystery in VC++ 6.0

对于下面的程序,我会得到不同的结果,具体取决于我是在Windows 7的VC ++ 6.0中以调试模式还是发布模式运行它。调试和发布行为的差异几乎总是表示处理指针和循环的错误,但我无法发现错误。

在调试模式下,我得到了我期待的结果:

Entered loop with i == 0, RecordCountNew == 0
RecordCountNew = 1 is positive.
Entered loop with i == 1, RecordCountNew == 1
Adding record with i == 1, RecordCountNew == 1
Added record with i == 1, RecordCountNew == 2
RecordCountNew = 3 is positive.
Entered loop with i == 2, RecordCountNew == 3
RecordCountNew = 4 is positive.
Finished loop with i == 3, RecordCountNew == 4

在发布模式下, 除了 RecordCountNew为正数的断言之外 ,我得到相同的结果:

Entered loop with i == 0, RecordCountNew == 0
RecordCountNew = 1 is positive.
Entered loop with i == 1, RecordCountNew == 1
Adding record with i == 1, RecordCountNew == 1
Added record with i == 1, RecordCountNew == 2
RecordCountNew = 3 is positive.
Entered loop with i == 2, RecordCountNew == 3
Finished loop with i == 3, RecordCountNew == 4

任何人都可以在他们的机器上复制这个,或者更好,解释一下吗?

#include <stdio.h>
#include <algorithm>

using namespace std;

struct record {
    int ID;
};

int RecordLimit;
record* Records = NULL;
record** RecordIndex = NULL;
record** RecordIndexNew = NULL;

int main(int argc, char* argv[]) {

    RecordLimit = 10;
    Records = new (nothrow) record[RecordLimit];
    RecordIndex = new (nothrow) record*[RecordLimit];
    RecordIndexNew = new (nothrow) record*[RecordLimit];

    int i;
    for (i = 0; i < RecordLimit; i++) {
        RecordIndex[i] = NULL;
        RecordIndexNew[i] = NULL;
    }

    int RecordCount = 0;
    for (i = 0; i < 3; i++) {
        Records[i].ID = i;
        RecordCount++;
    }

    int RecordCountNew = 0;
    for (i = 0; i < RecordCount; i++) {

        printf("Entered loop with i == %d, RecordCountNew == %d\n", i, RecordCountNew);

        RecordIndexNew[RecordCountNew] = RecordIndex[i];

        bool AddNewRecord = (i == 1);

        if (AddNewRecord) {
            printf("Adding record with i == %d, RecordCountNew == %d\n", i, RecordCountNew);
            Records[RecordCount + (RecordCountNew - i)].ID = RecordCount + (RecordCountNew - i);
            RecordIndexNew[RecordCountNew + 1] = RecordIndexNew[RecordCountNew];
            RecordIndexNew[RecordCountNew] = &Records[RecordCount + (RecordCountNew - i)];
            RecordCountNew++;
            printf("Added record with i == %d, RecordCountNew == %d\n", i, RecordCountNew);
        }

        RecordCountNew++;
        if (RecordCountNew > 0) printf("RecordCountNew == %d is positive.\n", RecordCountNew);
    }

    printf("Finished loop with i == %d, RecordCountNew == %d\n", i, RecordCountNew);

    delete[] Records;
    delete[] RecordIndex;
    delete[] RecordIndexNew;

    return 0;
}

(更正先前注释的更正):VC6 ++ SP6中的类似结果,但我根本没有“正面”输出。 我要去看看。 我们会看看能不能找到任何东西。 没有承诺(Euro Micelli)

我已经复制了相同的结果(发布时没有任何结果)@EuroMicelli发现。 但是,如果将RecordCountNew声明为volatile,则输出存在:

volatile int RecordCountNew = 0;

对于您的信息,volatile是一个关键字,它告诉编译器可以在随机时间(例如在CPU中断期间)从外部修改变量,并防止编译器积极优化其周围的代码。

tldrMSVC6错误地优化了RecordCountNew

PS:将RecordCountNew声明为short而不是int使打印输出重新出现。 你永远不知道20年前编译器的大脑里发生了什么。

PPS:因为我被要求解释这个bug,这里是正确输出的反汇编版本:

在此输入图像描述

edi寄存器存储RecordCountNew值, test指令命令跳转到printf 但是,这是OP的编译版本:

在此输入图像描述

test条件是在基指针寄存器ebp ,它与RecordCountNew无关。 根据ebp的值,程序每次输出行,或从不输出。

暂无
暂无

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

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