internals of cout?

Started by
8 comments, last by SimonForsman 12 years, 10 months ago
im curious as to if anyone knows the internals of how cout works?

for example the step by step internals of what happens when executing the following statement in c++

cout << "hello world" << endl;

-thx
Advertisement
Fire up your debugger, step through the code, and find out!
I did there r just certain function calls I'm unsure about
Like what, specifically?
cout is a class object (I think, of ostream) that is part of the C++ standard library. When you use cout you usually use it along with the stream insertion operator <<. Whatever class cout belongs to, I think it's ostream, ostream has overloaded operator <<, with many different signatures, most likely the primitive types.

So, overloading operator<< is basically the same thing as:

cout.operator<<( "hello world" );

As for how it actually prints characters to the screen I have no idea. Probably assembly language type stuff going on there.
Well, in the end, its still writing to stdout. stdout is a buffered output stream, so what its probably doing somewhere is pushing (don't know how the buffer works in detail so I can't be certain) the specified stuff into the output buffer which is dumped to screen 1) when the system gets around to it or 2) When it sees \n... I think... I quite imagine there are some OS level calls going on somewhere under the hood (posix API stuff on Linux, windows stuff on windows)
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
It can be (only as) a useful learning exercise to implement your own std::cout using printf, as a way to get your head around operator overloading. As has been said, << is an overloaded operator. A naive implementation might look like this:

[source lang="c++"]
class myostream
{
public:
myostream &operator<<(const char *s){ printf(s); return *this; }
};
[/source]

As the method returns a reference to itself, the following line is converted to the following:

[source lang="c++"]
myostream o;

o << "hello" << " " << "world";

// equivalent to
o.operator<<("hello").operator<<(" ").operator<<("world");

// equivalent to
o.operator<<("hello");
o.operator<<(" ");
o.operator<<("world");
[/source]

Things like std::endl are a slightly more complex case to be examined when you are comfortable with the above.
Be sure to research the std::streambuf class, it is one of the classes that interacts with std::stream instances. Here is an interesting article that Mr Edd (a forum member) wrote about stream buffers.

What is it you want to know in particular? Are you interested in how the various types get serialised into characters, or how the characters get queued for output, or the specific way std::cout is tied to the console on your compiler/operating system of choice?

im curious as to if anyone knows the internals of how cout works?
Nope. Don't know, don't care.
I don't know exactly how a modern car works either, and I still manage to get from A to B.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

im curious as to if anyone knows the internals of how cout works?

for example the step by step internals of what happens when executing the following statement in c++

cout << "hello world" << endl;

-thx

http://gcc.gnu.org/libstdc++/

Thats one way it can work. (Other vendors c++ libraries most likely have different implementations)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement