调试和释放模式提供不同的输出

时间:2011-03-22 03:22:34

标签: visual-c++

我的程序中有一个函数输出一个数据结构,该结构由两种格式的三个双精度组成,一个是文本,一个是二进制。

当我在调试和释放模式下运行程序时,我最终会得到不同的二进制输出但是文本输出相同。发生了什么事?

这是二进制输出代码:

void outputPoints(xyz* points, string description, int length, param parameters)
{

    stringstream index;
    index.str("");
    index << setw( 3 ) << setfill( '0' ) << parameters.stage;

    string outputName = parameters.baseFileName + " " + index.str() + " " + description + ".bin"; // create file name

    ofstream output; // create output object

    cout << "Output " << outputName.c_str() << "...";

    output.open(outputName.c_str(), std::ios::binary | std::ios::out); // open or create file for output
    output.write(reinterpret_cast<char*>(points), (sizeof(xyz) * length));
    output.close(); // close output object

    cout << "done" << endl;
}

2 个答案:

答案 0 :(得分:1)

调试版本通常使用某些模式初始化变量。通常分配的数据具有内容CDCD,删除的对象被FEEE覆盖。初始化变量时会覆盖CDCD模式。发布版本不会使用这些模式进行初始化。

检查程序中是否有未初始化的变量是值得的。您可以定义一个Dump函数,它只打印可疑变量的(前几个字节)。

答案 1 :(得分:0)

我不知道您是否为您的问题找到了解决方案,但我没有查看您的代码。 我有同样的问题,因为我添加了unsigned char和unsigned short并保存到unsigned short。我将所有变量都更改为unsigned short,问题就解决了。