#Program crash on std::cout

52 messages · Page 1 of 1 (latest)

sweet rover
#

The simple code that's reproducing issue for me is below.

#include <cstdio>
#include <iostream>

int main() {
    // Using printf instead of cout
    printf("test printf!\n");
    std::cout << "test cout!\n";
    printf("test printf2!\n");
    getchar();
    return 0;
}

It's printing test printf! and closing.
Program is compiled and started with g++ main.cpp && .\a.exe (g++ showed no issues in the process).

How do I debug something like that?

tawny fiberBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sweet rover
#

;compile

#include <cstdio>
#include <iostream>

int main() {
    // Using printf instead of cout
    printf("test printf!\n");
    std::cout << "test cout!\n";
    printf("test printf2!\n");
    getchar();
    return 0;
}
stable flickerBOT
#
Program Output
test printf!
test cout!
test printf2!
hard thorn
#

when you mean crash, do you mean hang?

#

becayse getchar is a blocking function

#

it will wait untill a key is pressed

sweet rover
#
#include <cstdio>
#include <iostream>

int main() {
    // Using printf instead of cout
    printf("test printf!\n");
    std::cout << "test cout!\n";
    // printf("test printf2!\n");
    // getchar();
    return 0;
}

is giving me the same result

#

test cout! is never printed

#

so I assume the problem is with std::cout

minor veldt
#

Random but what happens if you open a new project and try the same code again?

sweet rover
#

I can send the exe file here if someone want to try it, maybe it's my pc issue somehow

tawny fiberBOT
#

@sweet rover Please do not send executable files

sweet rover
#

okay 🤔

wicked zinc
#

Humn... I don't think there's "crashing" involved here. You are trying to open the .exe directly or using the release build configuration?

minor veldt
sweet rover
wicked zinc
#

At first glance I thought that maybe your issue was to do with some input and the getchar() picking up a trailing newline, though It isn't, my bad. I can't replicate your issue hmmge Let's check something, try inserting std::endl after std::cout

#

'\n' should flush the buffer (at least it does in some implementations), but maybe it isn't in g++

quiet ruin
#

@sweet rover How are you compiling

#

There's nothing wrong with this code

sweet rover
#

no errors

quiet ruin
#

Can you run (get-command g++).Path in powershell

#

Then run echo $env:PATH

sweet rover
#

sure, just to point out that it is printing test printf! so it's kind of working until cout

sweet rover
quiet ruin
#

Is that all?

sweet rover
#

there are bunch of other paths on PATH though

#

no

quiet ruin
#

Send the full output.

#

Ok

#

Try moving C:\ProgramData\mingw64\bin to the front of the path

#

Then you may need another path for dlls

#

Maybe C:\ProgramData\mingw64\lib, but you'd have to check

sweet rover
#

Also tried to add C:\ProgramData\mingw64\lib and C:\ProgramData\mingw64\include

#

but what helped is to move the C:\ProgramData\mingw64\bin to the system Path instead of user's and move it to the top

quiet ruin
#

Great

quiet ruin
sweet rover
#

though I don't have any g++.exe on the computer I guess it's using some executable from that bin folder in the process

quiet ruin
#

It was loading dlls it found elsewhere in the path, but not the mingw dlls your program was built to use

sweet rover
#

makes sense

#

I wonder if now is something else in the system will find mingw dlls in that folder and break 🤔🤔

#

I think I can compile it with bat file below to make it more safe

SET PATH=C:\ProgramData\mingw64\bin\;%PATH%
g++ main.cpp && .\a.exe
#

thanks Zelis again!

tawny fiberBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity