Sign in to follow this  
shoez

internals of cout?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Be sure to research the std::streambuf class, it is one of the classes that interacts with std::stream instances. [url="http://www.mr-edd.co.uk/blog/beginners_guide_streambuf"]Here[/url] 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?

Share this post


Link to post
Share on other sites
[quote name='shoez' timestamp='1307143461' post='4819255']
im curious as to if anyone knows the internals of how cout works?[/quote]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.

Share this post


Link to post
Share on other sites
[quote name='shoez' timestamp='1307143461' post='4819255']
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
[/quote]
http://gcc.gnu.org/libstdc++/

Thats one way it can work. (Other vendors c++ libraries most likely have different implementations)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this