Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c++ - std::endl crashes Windows 8, compiled using MinGW

I have 3 computers, two of which use Windows 8. Using the latest version of MinGW's g++ (4.8.1-4) my hello world program freezes whenever I compile and run on the Windows 8 computers but not in Windows 7.

#include <iostream>
int main()
{
    std::cout << "Hello, World!" <<std::endl;
    return 0;
}

This compiles just fine in g++ but running a.exe will display "Hello, World!" then a window will pop up and say "a.exe has stopped working, Windows can check online for a solution to the program...." etc.

Has anybody seen this problem.

Also, I tried "std::cout << "Hello, World! " << std::flush;" and this has the same problem. It seems that every function that flushes the buffer causes a crash.

Following Eric's advice, I recompiled the program and ran it in gdb and got the following output:

Program received signal SIGILL, Illegal instruction. 
0x00405065 in _Jv_RegisterClasses ()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the second instance, the ' ' should cause an output flush in any case, although in Windows I believe console output is immediate (or perhaps automatic after a short timeout) in any case without an explicit flush.

I suggest the following experiments:

1) See if it is specific to the C++ library by using the C library (in MinGW Microsoft's C runtime is used rather than glibc):

#include <stdio.h>
int main()
{
    printf( "Hello, World!
" ) ;
    return 0;
}

2) Eliminate the exit code by:

int main()
{
    return 0;
}

3) No newline at all:

#include <iostream>
int main()
{
    std::cout << "Hello, World! ;
    return 0;
}

4) Try different compiler options such as optimisation levels, or -fno-builtin for example, or as suggested here: -static-libgcc -static-libstdc++ (although I doubt ``-static-libgcc` will itself have any effect since MinGW uses Microsoft's C runtime DLL and the static library is only available with Microsoft's tools).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...