Deep C (and C++)

Programming is hard. Programming correct C and C++ is particularly hard. Indeed, both in C and certainly in C++, it is uncommon to see a screenful containing only well defined and conforming code. Why do professional programmers write code like this? Because most programmers do not have a deep understanding of the language they are using. While they sometimes know that certain things are undefined or unspecified, they often do not know why it is so. In these slides we will study small code snippets in C and C++, and use them to discuss the fundamental building blocks, limitations and underlying design philosophies of these wonderful but dangerous programming languages.

Jon Jagger and I just released a slide deck to discuss the fundamentals of C and C++ (slideshare, pdf).

23 Responses to Deep C (and C++)

  1. Nice, I have learned alot. Great Job.

  2. Really enjoyed this, well put together and I learnt a lot. I didn’t even get the answers the amateur programmer was giving.

  3. Great presentation! I used to love discovering the intricacies of of C++ bacl when I was studying, reading Meyers’ and Sutters books, looking things up in the standard … this presentation really took me back in time! :)

  4. Me says:

    Man, this was the coolest presentation I have ever seen in my life.
    Wonderfull. I hope you keep doing this work…

  5. Great work! What is the license of this presentation?
    I want to translate this into Japanese so if it is possible please send me a copy of original file (ppt, keynote etc.)

  6. Siah (@siah) says:

    Great set of slides thanks for sharing

  7. […] all the underlying facts already. In fact this is true about most things in IT: if you grasp the underlying principles then, even if you don’t know the specific command or syntax‡ off the top of your head, […]

  8. Collin says:

    Outstanding! Wish I could be at tonight’s presentation!

  9. Pandurangan R S says:

    Cool presentation.. I liked it.

    So why doesn’t the compiler reorder the members in the structure to optimize memory usage, and execution speed?

    Ans: Some languages actually do that, but C and C++ don’t.

    Is it better to say “Some languages actually allow it, but C and C++ standards don’t allow that. Perhaps this can be implemented as a non-standard behavior in some C (or C++) compiler”

    • Jeffrey says:

      C, when used for systems programming, needs to be able to map structures to external data, such as a block read from disk or bytes read from the network. A language that silently shuffled fields would complicate efforts such efforts.

      Also, the ordering of fields is often exploited in system code. Consider the so-called “struct hack,” used for declaring a variable length array at the end of the structure. This construct would break if the fields were arranged so that the array of length one at the end of structure were moved to any other place. (Yes, the “struct hack” is officially undefined in C89, but there was no other way to express that construct prior to C99, so you’ll see system code that uses it. See the C rationale for details.)

      Fundamentally, C trusts the programmer to say what the programmer wants.

  10. That was a wonderful talk the other night at the ACCU meeting, thank you! I especially liked that you advocated conceptual understanding of the language(s) over standard practices that are memorized by rote. There is no substitute for deep, conceptual thinking; we are mired in mediocrity without it.

  11. The Other Eugenicist says:

    Were you just following the current advertisers’ propensity to depict men as technologically stupid and women as technologically stupid or did you decide to do it yourself?

    I am beginning to find it offensive and it certainly does not mirror my experience in Silicon Valley, where women make up no more than 20% of the software engineering force, and, the majority of those are Chinese and Indian females.

    • Maybe it is a plot to get more women into informatics. In that case: Go for it, Jon and Olve!

    • Moschops says:

      When my mother moved to this country (which is not the U.S., thank you) there could be found signs in boarding houses saying “No blacks or Irish”. When you commonly see signs saying “No men” in hotel windows, come back to complain. Otherwise, take your ridiculous sense of victimhood back to your extraordinarily male-privileged life (when exactly was the last time a job-interviewer spent the interview staring at your chest and ignoring your words?) and stop seeing yourself as a victim.

  12. The Other Eugenicist says:

    Hmmm, that should have been “… women as technologically superior …”

  13. Nice Post says:

    Nice Post…

    […]Deep C (and C++) « Geektalk[…]…

  14. Jamie Hutton says:

    Really interesting slides, thanks!

  15. That’s really great! Many thanks for sharing!

  16. jigdi says:

    Very helpful. Thanks.

  17. I’m enjoying your Deep C presentation for Mar 2013. There is no scenario I can come up with for page 108, where the answer is 42.
    In the assignment to j, the index of v is i prior to increment, so v[i++] must be 2, with side effect of i being 3 after evaluation. The i*3 portion is either 2*3 or 3*3 depending on whether it is evaluated before or after the v[i++] . So j can only be 6+2 = 8 or 9+2 = 11.

    Have I missed something?

  18. Drahflow says:

    The answer on slide 304 is not actually giving the whole picture. The vtable-pointer is de-facto at address 0 from the struct start. Hence no alignment would be needed per-se. A padding to 24 bytes makes sense only, once you consider arrays of Xs.

%d bloggers like this: