« I think Joe saw us in the movie theater last night | Main | We have IPv6 telephones »

It's good to be a gangsta

Some hard-learned C++ knowledge:

cin always uses file descriptor 0.
cout always uses file descriptor 1.
cerr always uses file descriptor 2.

This is particularly annoying where you close file descriptors 0, 1, and 2 and re-open them to something else, because cin/cout/cerr will still use those file descriptors, and will read/write to the new things that you opened!

I finally figured out that that was what was happening on my tree-based booters -- I had debugging cerr's that ended up writing down sockets, which caused havoc on the remote side, because it got unformatted and unexpected messages. Doh!!!

In hindsight, this completely makes sense (and may even be by design; I don't have an iostream book handy). Consider that cin/cout/cerr are not tied to the OS -- they have no way of knowing when file descriptors 0, 1, and 2 have been closed and reopened to something else. For example, cout's operator<<(...) assumedly eventually boils down to:

write(1, ...);

In which case, cout has had no indication that file descriptor has been closed and re-opened into something else.

Just a point of wisdom for readers out there... it caused me three days of grief.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


This page contains a single entry from the blog posted on February 21, 2001 10:51 AM.

The previous post in this blog was I think Joe saw us in the movie theater last night.

The next post in this blog is We have IPv6 telephones.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34