Jump to content
  • Advertisement
Sign in to follow this  
KnightAdz

What c++ does for you, behind the scenes

This topic is 3209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!