The author presented this code under the title A bus error on my platform
#include <fstream>
#include <iostream>
int main()
{
std::ofstream log("oops.log");
std::cout.rdbuf(log.rdbuf());
std::cout << "Oops!\n";
return 0;
}
The string “Oops!\n” is printed to the file “oops.log”. The code doesn’t restore cout’s streambuf, but VS2010 didn’t report a runtime error.
Since
logandstd::coutshare a buffer, that buffer will probably be freed twice (once whenloggoes out of scope, then once more when the program terminates).This results in undefined behavior, so it’s hard to tell the exact reason why it triggers a bus error on his machine but silently fails on yours.