I'm working on a project where I call a function which triggers a segfault. I fixed this, but during the process I noticed the following.
When my code is of the format;
main(){
...
std::cout << "Looking for segfault\n"; // this does not print
buggyFunction(); // crashes in here
...
}
buggyFunction(){
...
thing_that_causes_segfault;
...
}
The line "Looking for segfault" doesn't print to STD, and the program crashes in buggyFunction. Fine, but when I add a cout line inside buggyFunction();
main(){
...
std::cout << "Looking for segfault\n"; // this now *does* print
buggyFunction();
...
}
buggyFunction(){
...
std::cout << "Now we're INSIDE buggy function\n"; // this prints too
thing_that_causes_segfault;
...
}
Inside buggy function, both lines print (and then it crashes).
Why do we see this difference in ouput, depending on the addition of this extra output call? Is it related to the handling of streams, or something else? I'm using g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3.