Arduino Forum

Community => Bar Sport => Topic started by: westfw on Nov 16, 2013, 03:22 am

Title: About low-level programming...
Post by: westfw on Nov 16, 2013, 03:22 am
http://research.microsoft.com/en-us/people/mickens/thenightwatch.pdf

I especially liked this bit about C++:
Quote
When it's 3 A.M., and you've been debugging for 12 hours,
and you encounter a virtual static friend protected volatile
templated function pointer, you want to go into hibernation and
awake as a werewolf and then find the people who wrote the
C++ standard and bring ruin to the things that they love
Title: Re: About low-level programming...
Post by: JChristensen on Nov 16, 2013, 03:54 pm
A Michigan man!  Very funny stuff, thanks for the link.  I also liked

Quote
...and ancient blood rituals that Bjarne Stroustrup performed at Stonehenge.
Title: Re: About low-level programming...
Post by: retrolefty on Nov 16, 2013, 04:46 pm

A Michigan man!  Very funny stuff, thanks for the link.  I also liked

Quote
...and ancient blood rituals that Bjarne Stroustrup performed at Stonehenge.



I've been told that on my side of the pond his name is pronounced Barney Shoestrap.  ;)

Lefty
Title: Re: About low-level programming...
Post by: JChristensen on Nov 16, 2013, 07:45 pm

Barney Shoestrap


:D XD 8)
Title: Re: About low-level programming...
Post by: SirNickity on Nov 20, 2013, 08:54 pm
I like C a lot.  It took years to decide to devote time to learn it, because I had been told when I was young that it was a weird and difficult language.  Now, I think it's elegant and straightforward.

But C++?  Man... I can't help but feel it just wasn't very well thought out.  Having classes and overloading is nice though...
Title: Re: About low-level programming...
Post by: nickgammon on Nov 20, 2013, 09:01 pm
Learn the standard template library (STL) and you'll fall in love with it. :)

It lets you do linked lists, vectors, maps, etc. really elegantly. And it saves you having to re-invent a linked-list method every time you come up with a new structure.

Plus, it has a *cough* string class.
Title: Re: About low-level programming...
Post by: liuzengqiang on Nov 22, 2013, 11:29 pm
My needs often dictates how much I remember. I learned C++ several times but didn't retain everything or half what I learned. If you ask me what implicit copy constructor does in an exam, I'll tell you the right thing only because I looked it up a week ago. But friends, and templates? I don't really remember nor did I find use yet. Programming arduino limits the use of lots of features in C++ that I would have use programming a PC.
Title: Re: About low-level programming...
Post by: westfw on Nov 23, 2013, 07:11 am
Quote
But friends, and templates? I don't really remember nor did I find use yet.

I can somewhat recommend this Free Online Class on "C++ for C programmers."
https://class.coursera.org/cplusplus4c-002

I mean, there are significant problems with this class.  The prof talks too slow, and will spend 10 minutes explaining how "swap" works, only to have a HW assignment along the lines of "implement an AI that uses a generalized undirected graph datastructure with colored edge weights and Djiksta's algorithm to play the game of Hex."  (did I say "along the lines of"?  I meant "exactly."  Grr.)
And apparently some of what he advocates is considered controversial (operator overloading?)

But on the other hand, he dives right into the things that are different than C and how and why they're useful.  "templates" got talked about in the first set of lectures (thus the annoying diversion into the history of "swap".)  Friends and inheritance and virtual functions are in there (whereas they tend to be either left as "advanced" topics or taken for granted in many books/classes that teach an OOP from the ground up.)  IOsteams properly introduced as an operator-overloaded shift...

I'm flunking.   But I'm getting a significant amount out of the class, and a lot of things have become much clearer to me...
I can live with that!
Title: Re: About low-level programming...
Post by: Boardburner2 on Nov 26, 2013, 05:09 pm
Quote

virtual static friend protected volatile templated function pointer,


I found that very funny, it is bar sport.
Having read subsequent posts im now thinking such a thing exists.
Gulp.
Title: Re: About low-level programming...
Post by: westfw on Nov 26, 2013, 05:17 pm
Well, every word is a C++ concept; I'm not quite sure whether they make sense all strung together.

OTOH, I've had to use tools to create/interpret C definitions for functions of stuff that returns (pointers to) arrays of function pointers, for real, and I'm pretty sure you can attach additional C++ concepts to those to make them even less intelligible...
Title: Re: About low-level programming...
Post by: liuzengqiang on Nov 26, 2013, 05:19 pm
I can understand function pointer and static friend. But what does it mean by virtual static friend then? If a function is static, can it be overridden by a class that inherits from the base class?

Where does volatile apply? The pointer is changing value so don't optimize code that accesses its value in a register or memory?

Templated is beyond my understanding. I don't use templates yet.
Title: Re: About low-level programming...
Post by: Boardburner2 on Nov 26, 2013, 07:36 pm
I did not try to make sense of it.
Function pointer was enough to put me off.
Title: Re: About low-level programming...
Post by: SirNickity on Nov 27, 2013, 10:46 pm
But how would you write plugin frameworks without function pointers?  :)  They're the same as variable pointers, just referring to functions that can be called by address instead of values that can be read by address.  Simple!

Templates?  No idea.  When I hear "template", I think of a stripped source file that can be customized to do something convenient without having to write it all from scratch.  I assume the C++ concept is vaguely related, although not necessarily.  "Overloading" sounds like something that would cause a compiler error, but it's actually a useful (if not sometimes obfuscating) feature, so who knows?

I'm a huge fan of clean, readable, intelligible code.  Anything that makes code harder to comprehend is detrimental, although occasionally necessary.  While I also like to write efficient code, if I have to pick between clarity and performance, it becomes an internal struggle weighted mostly by the number of times the given code will be run in a loop, or whether there will likely be events depending on its completion in the shortest possible time.

C++ just reeks of a language developed with all the latest theory and patterns, while being early enough that it didn't have the benefit of learning from the dubious attempts by many other well-established languages trying to achieve similar goals.  (I could be way off -- I'm not a comp-sci history buff.)  It's almost like you can see the parts where C++ departs from C syntax the same way you can tell when somebody added a room to a house 20 years after it was constructed.  This side?  Elegant, logical, well-planned and solid.  This side?  The contractor forgot to let the foundation settle, and the AC outlets all tap off the light switch in the adjacent bathroom, but the paint's fresh and vibrant!

Then there's PHP, which has absolutely no excuse.  Sheesh.  Is this perl or C?  Nobody knows.
Title: Re: About low-level programming...
Post by: westfw on Nov 28, 2013, 06:38 am
Templates are essentially a mechanism for making decisions at compile time that is much more powerful than preprocessor macros, and more HLL-aware than the usual assembler macros.
So you can do something like:
Code: [Select]
template <typename swappable_t>
void swap(swappable_t *a, swappable_t *b)
{
   swappable_t temp;
   temp = *a;
   *b = *a;
   *b = temp;
}

And then if you have
Code: [Select]

int a, b;
student_t s1, s2;
  :
     swap(&a, &b);
     swap(&s1, &s2);

The first statement will cause a version of swap that works on ints to be compiled, and the second will cause a version of swap that works on students to be compiled.
I guess it can get a lot more complicated than that; templates are apparently a turing-complete programming language all by themselves.  So they can do anything if you're willing to be obscure enough.  (and thus, back to the sort of complaints in the original article!)

Title: Re: About low-level programming...
Post by: kf2qd on Dec 02, 2013, 10:59 pm
C is known to be dangerous, so most go in expecting trouble. If they don't find it? GREAT!!! But at least YOU WERE WARNED.

C++ was formed by a committee to make C safe. So poor blighters go in expecting a great experience and get devoured by beasts that never have been described. And if one survives, one has know way of knowing how one survived,it's as if all memories were erased.
Title: Re: About low-level programming...
Post by: waterlubber on Dec 04, 2013, 12:48 am
Quote
I can understand function pointer and static friend. But what does it mean by virtual static friend then? If a function is static, can it be overridden by a class that inherits from the base class?


Quote

The first statement will cause a version of swap that works on ints to be compiled, and the second will cause a version of swap that works on students to be compiled.


POP! (That's a brain explosion)


---And if you think that's hard, try coding Android ADT (I couldn't get a FUDGING BUTTON to work.)

---And if you think the ADT is hard, try taking a state test. You need a PhD in Cryptology AND in Psychiatry to figure that out.
Title: Re: About low-level programming...
Post by: pYro_65 on Dec 04, 2013, 01:27 am

I can understand function pointer and static friend. But what does it mean by virtual static friend then? If a function is static, can it be overridden by a class that inherits from the base class?

Where does volatile apply? The pointer is changing value so don't optimize code that accesses its value in a register or memory?

Templated is beyond my understanding. I don't use templates yet.


Hmmm, there are a few things not right, I wouldn't take that little quote as anything more than humor.

Static friend is redundant, static or friend should be used,
You cannot take pointers to template functions only a particular instantiation:  foo<>, foo<int>, which is not a template any more but a fully defined entity.
Virtual static is wrong, a static non-member is not bound to any object and therefore has no 'virtual' implications. This also applies to friend, these keywords imply a non-member entity.
Title: Re: About low-level programming...
Post by: westfw on Dec 04, 2013, 03:44 am
Quote
POP! (That's a brain explosion)

What?  In C, you'd have a swap function to swap two integers, so that (1, 2) would become (2,1):
Code: [Select]
void swap(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
   *b = temp;
}

But if you wanted to have a library of swap functions for different datatypes, you'd need to implement swap_int(), swap_long(), swap_float(), swap_string(), and so on.  Plus, any data types that you defined yourself.
Code: [Select]
void swap_string(char **a, int **b) {
  char * temp;
  temp = *a;
  *a = *b;
   *b = temp;
}


C++ would make this somewhat easier, because you can have 'function overloading", where the same function name with different arguments results in separate code:
Code: [Select]
void swap(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
   *b = temp;
}
void swap(char **a, int **b) {
  char * temp;
  temp = *a;
  *a = *b;
   *b = temp;
}

(now, the top-level code can say "swap(a,b)" for several types of a and b.)
But - note that the actual CODE in those functions stays the same regardless of the types of arguments, except for the type specifications in the header and for "temp"; what you really want, so you can avoid rewriting the same code over and over, is to somehow pass that part of the function - the type - as an argument to some sort of macro.  (I could almost imagine this as a C preprocessor macro, at least for an inline swap.  But... Ugly!)
Code: [Select]
template <typename my_type>
void swap(my_type *a, my_type *b)
{
   my_type temp;
   temp = *a;
   *b = *a;
   *b = temp;
}

This is what C++ templates give you.  (one of the things they give you, anyway.)  It's made more useful by also providing a "Standard Template Library" (STL) that provides a lot of common data structures and algorithms.  So you can SORT your VECTOR of (user-defined) THING_TYPES without having to write any code for SORT, or VECTOR (a growable array-like thing) that is specific to THING_TYPE (other than a "compare-to" function.)  (again, you can almost do this in C, given (dangerous) void* pointers, macros, and so on.  But the C++ version is much prettier and less subject to being wrong.)
Title: Re: About low-level programming...
Post by: SirNickity on Dec 05, 2013, 12:02 am
Oh.. that's kinda cool.  :)

I have to admit, I've been tempted to use a C++ compiler on my C programs just to take advantage of function overloading.  That's one heckuva useful tool.
Title: Re: About low-level programming...
Post by: JChristensen on Dec 05, 2013, 12:14 am

I have to admit, I've been tempted to use a C++ compiler on my C programs just to take advantage of function overloading.  That's one heckuva useful tool.


A slippery slope that may be, yesssss. Leads to the Dark Side, it does  ;)
Title: Re: About low-level programming...
Post by: udoklein on Dec 06, 2013, 09:38 pm
If you really want to make your mind explode try to analyze template metaprogrammed code. After reading about template metaprogramming I wondered if I could achieve similar effects with the macro processor. Of course this is not the same but close http://blog.blinkenlight.net/experiments/removing-flicker/knight-rider-no-flicker/ (http://blog.blinkenlight.net/experiments/removing-flicker/knight-rider-no-flicker/) ;)
Title: Re: About low-level programming...
Post by: nickgammon on Dec 06, 2013, 11:12 pm
Templates are extremely useful, particularly if you get into the Standard Template Library (STL).

I admit it can be a little hard to get started, but once you do, your life is so much easier. Linked lists, maps, vectors, strings, all much much easier than writing yourself.

Here's a simple example. Say you have a structure of widgets, and you want to have any number of them in a linked list:

Code: [Select]

#include <iterator>
#include <list>
#include <pnew.cpp>

typedef struct
 {
 int foo;
 char bar;
 byte fubar [10];
 }  widget;  

std::list<widget *> myList;
 
void setup ()
{
 widget * w;
 
 w = new widget;
 myList.push_back (w);
 w = new widget;
 myList.push_back (w);  
}  // end of setup

void loop () {}


Done. You can add to the front or the back of the list. You can traverse the list forwards or backwards. If you find a particular item you can delete it from the middle, front or back. You don't need to muck around with "next" or "previous" pointers, like you would if you do it manually.

You have other container types like maps (for key/value pairs), vectors (where you want to go straight to item 42), and so on. Queues and stacks are variants on vectors, however there is a deque type (double-ended queue) which is more efficient for those applications.

There are various utilities in the library, for example you can sort containers, or do things like delete every entry matching a certain condition.

Containers can be pointers (as in the example above) or actual data (not pointers) provided they can be copied and assigned.

Below is a similar example, however this time we use widget directly (not a pointer to widget):

Code: [Select]

#include <iterator>
#include <list>
#include <pnew.cpp>

typedef struct
  {
  int foo;
  char bar;
  byte fubar [10];
  }  widget; 

std::list<widget> myList;
 
void setup ()
{
  widget w;
 
  myList.push_back (w);
  myList.push_back (w);
   
}  // end of setup

void loop () {}
Title: Re: About low-level programming...
Post by: SirNickity on Dec 07, 2013, 01:15 am
I wrote some of my own libraries (a reinvented wheel no doubt) to handle lists and ring buffers and that sort of thing.  Easy enough, but yeah, not having to do it at all is easier.  Not to mention the maintenance is done for you, and everyone knows how it works just by seeing the initialization.

Key/value pairs are one thing I miss when writing C after working in scripting and web development languages.  I love hashes.  Very handy for indexing and validity checks ($sanitized = $valid_terms[$input]) etc.  Inefficient as all heck, I'm sure...