Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview with Bjarne Stroustrup - abstraction and hand-crafted code

Tags:

c++

I have read an interview with Bjarne Stroustrup on C++ and its design. I was confused by the terminology he used there so I was hoping to get some of such moments clarified.

I documented my design aims and the constraints on the design in my book “The Design and Implementation of C++” and in my two History of Programming Languages conference papers, but briefly, I aim for

  • zero overhead compared to hand-crafted code when using abstractions,
  • a machine model very similar to that of C,
  • an extremely flexible set of abstraction mechanism, and
  • static type safety.
  1. What does "hand-crafted code" mean in this case?.. Does Stroustrup mean some hand-crafted tool for an abstraction concept a programer could create?
  2. What does "machine model" mean in this case? A way a language interact with hardware?

I took a look on the book he mentioned (which is actually “The Design and Evolution of C++”, I believe) but I'm still not really sure. These two terms are also not googled very well.

like image 632
kazarey Avatar asked Nov 22 '13 00:11

kazarey


Video Answer


2 Answers

"Machine model" means the ways in which the computer manifests itself in the language. So for example, C and C++ treat bytes, memory locations, pointers, integer types in more or less the same ways.

"Hand-crafted" here means equivalent code that does not use the abstraction provided in C++, and is written efficiently in C or assembly (or C++, just excluding that abstraction). This is an "aim", not a "constraint" -- C++ does not precisely achieve it in all cases.

So just as an example, vector<int> does not always achieve exactly the same performance as a similar resizable array written using malloc and free, because that code could (and naturally would) use realloc to resize the storage when necessary. realloc doesn't very often save you any time, but when it does it saves a chunk. vector cannot do that, because the allocator interface has nothing equivalent to realloc, and hence has introduced a small overhead. But vector has roughly the same performance, almost all of the time, so the aim of zero-overhead is nearly met.

For another example: with a decent C++ compiler in release mode, writing myvector_of_int[3] genuinely has zero overhead compared with writing my_int_pointer[3]. It may not be immediately obvious why accessing a data member of a local variable (the data pointer stored in myvector_of_int) has no overhead compared with accessing a local variable of pointer type (my_int_pointer), but it needn't cost anything extra.

like image 157
Steve Jessop Avatar answered Oct 12 '22 23:10

Steve Jessop


By "hand-crafted" code, Stroustrup means a hand-written implementation of the abstraction; for example, a hand-crafted for loop would be N copies of the loop body. (See Duff's Device for a non-trivial implementation.)

And by "machine model", Stroustrup means the compiler's view of the machine. For example, C and C++ provide roughly equivalent views of bytes, memory locations, memory access ordering, and so forth; Fortran, by contrast, doesn't have any concept of "bytes", and memory locations (or array accessing, for that matter) doesn't correspond well to the C/C++ view at all.

like image 33
Eric Brown Avatar answered Oct 13 '22 00:10

Eric Brown