What c++ does for you, behind the scenes

Started by
21 comments, last by visitor 14 years, 5 months ago
I was talking to someone in the game industry the other day, and he mentioned that (in his opinion) good c++ programmers think about how much c++ does for you, i.e. how it knows where to find the correct implementation of a virtual function etc. and how to decrease the amount of code that is created by the compiler to handle this. (this is obviously quite important in console programming). I was just wondering if anyone knows of any good links where I could read up on this? Thank you, Adam
Advertisement
Unfortunately, your source is either ignorant of certain facts about C++, or glossing over them in speaking to you (or you missed them). I'll give him the benefit of the doubt and go with the latter.

Much of the things he is alluding to, such as the implementation of virtual functions and dynamic dispatch, are not called out by C++. They are, in fact, compiler-specific. Similarly, other things end up being platform specific (that is, dependent on the OS version or chip architecture). For Windows, MSDN contains a lot of this stuff directly, and typically has links to other useful places as well.

The best source for this information is thus the technical documentation for the OS, chip, or compiler in question... again, stressing the fact that this is no longer really having anything to do with C++.

For actual C++-related stuff, you can look for drafts of the standard. Or just buy yourself a copy. There also the C++ FAQ Lite.

You usually don't need to worry about this kind of thing though. It's mostly a curiosity for beginners, and when you need to know about it you will probably have experienced it first-hand. That's how good C++ programmers think about this stuff -- not because good C++ programmers think about it, but because good programmers (who use C++ or perhaps other languages) have usually experienced it.

Certainly, indulge your curiosity about it if you like -- but don't think it's important for you to start caring about it in order to learn C++ effectively.
It can also be pretty instructive to look at the generated assembly produced by your compiler. Most compilers have switches to compile to assembly rather than object code. For MSVC it's the /FA family of switches and gcc uses -S.
I probably misheard some of what was being said, as it was a loud environment. Thank you for your advice though, I guess I will keep it in mind, but until I am working with one specific compiler/chip/OS there is probably not a lot of point in me knowing much about it, as I probably wouldn't have an effective way of reducing the workload anyway.

SiCrane, I will look into the assembly creation out of curiosity I think!

I appreciate your fast responses.
It is still good to know, how the C++ compiler does this or that. A good source for information is The Design and Evolution of C++. The book is several years old, but it is still an interesting read.
Quote:Original post by jpetrie
You usually don't need to worry about this kind of thing though. It's mostly a curiosity for beginners, and when you need to know about it you will probably have experienced it first-hand.
Ehhhh.

The internal implementation of virtual functions is hands down the most common interview question I've ever gotten, and I've frequently been grilled on all kinds of internal details and standard behavior details. The most extreme version was when I was basically asked to reimplement a bunch of C++ features (virtual calls, inheritance, multiple inheritance, etc) in strict C.

I suppose the practical use of this type of knowledge is debatable, although personally I like having it in my arsenal. But if getting a job is included in "worry about this kind of thing", then it's awfully helpful.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
While I agree that the knowledge is important, I think it tends to come with experience rather than needing to be sought out, particularly at the early stages of programming.

I have never gone looking for lots of in depth information on how C++ features are implemented under the hood. Yet, through my time reading and learning about C++ and especially listening to some of the smart people like yourself here on gamedev.net I have picked up most of the details.

I think that was what jpetrie was alluding to in his second last paragraph.

A beginner typically has a mental model of a computer that is incorrect enough that delving into this sort of detail too early can do more harm than good. We see this sometimes when beginners over worry about the cost of piddly virtual functions when the frequency of the calls is so low that its not worth worrying, without looking at the "big picture" of optimisation that tends to have a more tangible effect.
Quote:Original post by Promit
The internal implementation of virtual functions is hands down the most common interview question I've ever gotten, and I've frequently been grilled on all kinds of internal details and standard behavior details. The most extreme version was when I was basically asked to reimplement a bunch of C++ features (virtual calls, inheritance, multiple inheritance, etc) in strict C.
Are you at liberty to divulge what sort of position you were interviewing for when these questions tend to arise?

My gut feeling is that you are much more likely to receive this type of question if you are applying for a systems programming position, or a job in embedded development (where the C++ compilers tend to be less capable).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

It sounds like what this guy was talking about was providing your own copy of the c std libs.

Some people do this to reduce the size of your .exe, although in most cases it really doesn't matter unless you are really really tight for executable size restrictions. For instance, an executable that doesn't do anything commonly weighs in at 40k. It goes beyond virtual functions and into all of the functions implicitly called for you like the registration, initialization, and destruction of the global objects amongst other things. Also providing implementations for whatever c std functions you call explicitly.

Regardless of being near useless from a practical standpoint it is decent knowledge to know about.

You can try googling libctiny for some efforts of others to reduce exe size but in a more generic way. Or try "size reduction executable c++"
// Full Sail graduate with a passion for games// This post in no way indicates my being awake when writing it
Quote:Original post by swiftcoder
Are you at liberty to divulge what sort of position you were interviewing for when these questions tend to arise?

My gut feeling is that you are much more likely to receive this type of question if you are applying for a systems programming position, or a job in embedded development (where the C++ compilers tend to be less capable).


I cannot speak for Promit, but I have seen this as a standard question as well for all engineers/programmers at these companies. One thing to realize is that the person asking the questions doesn't always expect you to get all of them right, but rather they want to see how extensive your knowledge goes. For the most part, how virtual functions work is taught in schools, but there are followup questions to see exactly how much you know about the inner workings.

Now, I am sure for some positions they do expect you to get the question correct.

// Full Sail graduate with a passion for games// This post in no way indicates my being awake when writing it

This topic is closed to new replies.

Advertisement