1 of 21

(*C99_today)++

Jonathan Gingras

2 of 21

Myths & Sophisms

  • Old (> 40 years old)
  • No objects (cuz I think there is Java, Python, .Crap, and Blah Blah...)
  • No abstraction
  • C is a low-level system programming language and it sucks at writing every day apps (same people telling: “Let’s do it in node gurls”)
  • No dynamism
  • No sugar syntax (this one is kind of true if you don’t know the preprocessor)

3 of 21

People’s fear : *POINTERS

  • Who fears pointers (and pointers of pointers of pointers)?

RAISE YOUR HANDS

  • Who just doesn’t get the point of it?

RAISE YOUR HANDS

  • Who ever had a nightmare of dereferencing a NULL and getting stabbed (literally) ?

4 of 21

What’s C about anyways ?

  • Freedom, compiler won’t tell you what to do
  • Knowing what you do (pretty manual)
  • NO OBSCURE MAGIC AT THE END! (I look at you Javascript programmers)

5 of 21

What’s a shared object?

  • Don’t think it’s an OO concept of objects plz…
  • Binary object (.o)
  • It is shared. (.so)
  • It is loadable.
  • Almost every programming language you like has its own system to load shared objects

6 of 21

Compile-time linking

  • -l to the compiler you know
  • binds executable
  • needs library absolute path
  • Most common linking type
  • Either static (archive) or dynamic (shared objects)

7 of 21

Overridable!!!

For shared objects

  • OS specific override loading of compile-time liked libraries
  • {LD_PRELOAD=... , DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=...}
    • THIS PARTICULAR POINT IS VERY IMPORTANT

8 of 21

Runtime loading : dlload/dlsym

  • Loading it at runtime, not only at compile time
  • Not only with a “prototype”
  • This is how you do plugins

  • DEMOS

9 of 21

UNIX API

  • What’s UNIX, I’m not kidding, some ppl think it’s Linux misspelled or something UNIX != Linux
  • What about POSIX and SUS?
  • The libc deals front-end (user-space-level) implementation
  • Kernel deals the back-end (system-calls) implementation
  • Let’s go deeper (Not a deep learning joke)

10 of 21

I/O : file descriptors

  • open/close
  • read/write
  • dup/dup2

File descriptors are simple ints

11 of 21

Pipes

  • What’s a pipe?
  • What’s the point

  • DEMO

12 of 21

Sockets

  • Liking web?, of course
  • On UNIX, your userspace programs use them for every connection

  • DEMO

13 of 21

C++ Part of the Presentation

14 of 21

Runtime Abstraction

  • What’s runtime abstraction in C++ : virtual
  • Can we do it in C? Yes
  • How? Wait a second
  • Do we usually do it this way? depends

15 of 21

How to deal in C

  • function pointers
  • int (*yass)(char *more_yasssss);
  • abstract structs of functions
  • fat pointers
  • etc.

16 of 21

how the virtual keyword works

  • What’s a vtable?
  • how to use it

  • DEMO

17 of 21

Abstraction

  • We talked earlier about runtime abstraction
  • What about compile-time abstraction?
  • Let’s show you what I mean

18 of 21

Basic templating

  • Most of you are familiar with common templating
  • Particularly from the STL (Standard Template Library)

template <typename T>

class SomeClass {

T* data;

...

};

template <typename T>

void some_function(T &object) {

...

}

19 of 21

Template specialization

  • Who knows them/ever used it? RAISE YOUR HANDS
  • What does it do?

template <typename T>

some_function(T &object) {� // code

}

template <>

some_fonction(size_t object) {

// other code

}

template <typename T>

class {

some_mtd();

};

template <>

class<size_t> {

some_other_mtd();

}

20 of 21

The “Trait” Pattern

  • Very C++-specific
  • Some new programming languages, such as Rust (which I don’t like) use this pattern, built-in their type system
  • Very powerful
  • Provides high performance, highly optimizable generated code for compile time abstraction
  • DEMO

21 of 21

The advantages of

C++11 templating

  • The “auto” keyword prevents a lot of problems at template-instantiation-time (much less public in-class typedefs to write)
  • Template-enabled “using” keyword
    • template <typename T> using vector_ptr = std::shared_ptr<std::vector<T> >;
  • Range-style for loops exploiting iterator-like objects cleanup the code style a lot.
  • And more.