Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C for Dynamic Language Devs

apeiron
January 12, 2013

C for Dynamic Language Devs

C isn't hard! Really. It's mostly similar to the languages you're already using, with a few extra bits for memory management. Even that is easier these days with things like garbage collection. In this talk I make an effort to show dynamic language developers that C is not as hard as they think.

apeiron

January 12, 2013
Tweet

Other Decks in Programming

Transcript

  1. C for Dynamic Language Devs This won't hurt a bit

    Or How I Learned to Stop Worrying and love DTrace Thursday, May 23, 13
  2. Today, We Learn C Airship Titanic, CC-BY 2.0 by Luis

    Daniel Carbia Cabeza at Flickr I'm Sure It'll Go Swimmingly Thursday, May 23, 13
  3. Similarities ❖ C is a curly brace language with semicolons

    ❖ Variables are declared before use ❖ Functions serve as main organizational unit ❖ Perhaps more than Perl, C is Unix and vice versa ❖ Libraries exist for most tasks Thursday, May 23, 13
  4. Data ❖ Integral types ❖ Floating point types ❖ Character

    types ❖ Aggregates ❖ Pointers Thursday, May 23, 13
  5. Integral Types ❖ Integers, the char type, long integers, short

    integers ❖ Comprised of different amounts of memory ❖ No floating point operations ❖ Can and will {over,under} flow ❖ Use GMP if you need arbitrarily large (>264) values Thursday, May 23, 13
  6. Pointers ❖ Not that hard ❖ A pointer contains a

    memory address ❖ The unary * operator gets the thing pointed to ❖ The unary & operator gets an object's memory address ❖ That's pretty much it Thursday, May 23, 13
  7. Strings ❖ Least fun part of C ❖ Null-terminated ❖

    Implemented as pointers ❖ Thankfully, lots of libraries to help Thursday, May 23, 13
  8. Functions ❖ Like in Perl but with types ❖ Real

    prototypes ❖ Similar calling conventions ❖ Push button, receive bacon ❖ Check manual section 3 Thursday, May 23, 13
  9. C's CPAN ❖ C has libraries ❖ Many Perl libraries

    build on C libraries ❖ A lanky British guy wrote something about picking libraries to use ❖ Do what he says Thursday, May 23, 13
  10. The Standard Library ❖ Ships with every C implementation ❖

    Covers some basics like string handing, maths, memory management, OS primitives ❖ Still very minimal: no JSON, no HTTP client Thursday, May 23, 13
  11. Where to find libraries ❖ Github ❖ SourceForge ❖ Vendor's

    packages ❖ Search engine ❖ Your favorite app's dependencies Thursday, May 23, 13
  12. Syscalls ❖ These do things that interact with hardware or

    the kernel ❖ Context switching ❖ Things like read(), write(), ioctl() ❖ But also time-related functions and other things ❖ Check manual section 2 Thursday, May 23, 13
  13. C Library Manuals ❖ Most good libraries have manuals ❖

    Might be in traditional roff format, might be HTML or something else ❖ Always read the manual for the version of the lib you're using Thursday, May 23, 13
  14. Tracing Tools ❖ Lots of different tools for different OSs

    ❖ All operate under the same premise ❖ Can trace already-running programs ❖ dtruss* ❖ truss ❖ ktrace ❖ strace * dtruss is part of the DTrace toolkit. DTrace is a scriptable tracing tool comprised of awesome and win. Thursday, May 23, 13
  15. ⸘WTF‽ ❖ These tools all output system calls ❖ Arguments

    ❖ Return values ❖ The best part: each and every one of these syscalls is documented (or should be) Thursday, May 23, 13
  16. Some Miscellany ❖ C apps have some boilerplate before /

    after running user code ❖ Shared library loading, etc. ❖ This is pretty system-specific, takes some experience to see past Thursday, May 23, 13
  17. How to Use the Info ❖ These tools cover syscalls,

    not user level functions* ❖ But they show you all the details about all the syscalls ❖ Read arguments and return values—they should make sense for what you're trying to debug ❖ These tools are useful not just for your code but also in general—find out why that module isn't loading e.g. Except DTrace, of course Thursday, May 23, 13