为什么以下C ++代码在发布模式下出错,但在调试模式下工作正常?

时间:2018-02-26 20:11:48

标签: io visual-studio-2017

我正在尝试从文件中读取2个整数并将其总和写入另一个文件。在发布模式下运行以下代码(Visual Studio 2017社区)

    // Kickstart.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>


int main()
{
    using namespace std;
    string inpath = "E:\\Pratik\\Kickstart\\Inputs\\Sum.txt";
    string outpath = "E:\\Pratik\\Kickstart\\Outputs\\Sum.txt";
    ifstream fin;
    ofstream fout;
    fin.open(inpath);
    fout.open(outpath);
    if (!fin)
    {
        cerr << "Unable to open input file";
        exit(1);
    }
    if (!fout)
    {
        cerr << "Unable to open output file";
        exit(1);
    }
    int sum = 0;
    int a, b;
    fin >> a >> b;
    cout << "a: " << a << "b: " << b << endl; //Line in question
    fout << a + b;
    fin.close();
    fout.close();
    system("pause");
    return 0;
}

我在发布模式(x64,本地Windows调试器)中运行它时出现此错误

1>------ Build started: Project: Kickstart, Configuration: Release x64 ------
1>Kickstart.cpp
1>Generating code
1>LINK : fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 258)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1>Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>  link!InvokeCompilerPass()+0x1f1b9
1>  link!InvokeCompilerPass()+0x1f1b9
1>  link!InvokeCompilerPass()+0x1ebd3
1>  link!DllGetC2Telemetry()+0x10769b
1>
1>LINK : fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 258)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1>Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>  link!InvokeCompilerPass()+0x1f1b9
1>  link!InvokeCompilerPass()+0x1ebd3
1>  link!DllGetC2Telemetry()+0x10769b
1>
1>
1>LINK : fatal error LNK1000: Internal error during IMAGE::BuildImage
1>
1>  Version 14.12.25831.0
1>
1>  ExceptionCode            = C0000005
1>  ExceptionFlags           = 00000000
1>  ExceptionAddress         = 063636E6 (05F30000) "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\HostX86\x64\c2.dll"
1>  NumberParameters         = 00000002
1>  ExceptionInformation[ 0] = 00000000
1>  ExceptionInformation[ 1] = 0FA6D358
1>
1>CONTEXT:
1>  Eax    = 0FA6D358  Esp    = 008FE9E8
1>  Ebx    = 05A500EC  Ebp    = 008FEA18
1>  Ecx    = 00000005  Esi    = 0FA6D358
1>  Edx    = 08C8C02C  Edi    = 008FE9F8
1>  Eip    = 063636E6  EFlags = 00010202
1>  SegCs  = 00000023  SegDs  = 0000002B
1>  SegSs  = 0000002B  SegEs  = 0000002B
1>  SegFs  = 00000053  SegGs  = 0000002B
1>  Dr0    = 00000000  Dr3    = 00000000
1>  Dr1    = 00000000  Dr6    = 00000000
1>  Dr2    = 00000000  Dr7    = 00000000
1>Done building project "Kickstart.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

在修改代码时,我发现注释掉cout行(有一个“有问题的行”注释)会使代码正常运行。我最初的怀疑是,因为2个变量被写入stdout流,所以它们不能写入ofstream。但是,当我注释掉“fout”&lt;&lt;“a”&lt;&lt;“b”行(“有问题的行”旁边)时,我得到了同样的错误。这意味着变量只是拒绝加载到stdout流中(我是否正确?)。我无法弄清楚为什么会这样。谷歌搜索“用C ++将变量写入多个流”也没有用。

现在,当我在Visual Studio中选择调试模式并应用断点(在字符串inpath声明和赋值中)并逐步执行代码(没有代码被注释。上面给出的代码按原样逐步执行),它运行成功。它在stdout(控制台屏幕)上打印a和b的值,并且总和也写在输出文件中。

所以这里发生了两件事: 1 - 在释放模式下,变量只能写入流,但不能写入控制台屏幕。

2 - 在调试模式下,代码的行为完全符合我的要求。

注意 - 输入文件中的内容是由空格分隔的2个数字(5和9)。我在Windows 10上运行Visual Studio 2017社区

有人可以解释这里究竟发生了什么吗?谢谢!

0 个答案:

没有答案