What is the advantage of C+?
lardom
Posts: 1,659
I still have a lot to learn about spin and I'm slowly learning how to read Pasm so I'm only asking out of curiosity. C++ appears to be a high level language like spin so I'm guessing its execution speed is similar to spin.
The propeller is capable of both parallel processing and assembly speed. Interrupts? In my mind it's like comparing fuel injection to horse shoes.
The prop has to be one of the best robotic controllers. A system where robots get their marching orders from a computer would be reason enough to learn C+. I'd like to understand that better but that's in the distant future.
The propeller is capable of both parallel processing and assembly speed. Interrupts? In my mind it's like comparing fuel injection to horse shoes.
The prop has to be one of the best robotic controllers. A system where robots get their marching orders from a computer would be reason enough to learn C+. I'd like to understand that better but that's in the distant future.
Comments
C++ has been around since the late 80's and boatloads of code is written in it. This resulted in C++ being the parent language of many modern languages (e.g. Java, JavaScript, C#, and Groovy). So C++'s syntax, operators, and organization are consider normal by a generation of programmers. When another language redefines those operators to a different meaning that is a headwind for programmers using that language. Spin's test and assignment operators which on the surface aren't a bad idea give fits to C++ programmers.
The longevity of C++ has also resulted in many open source projects adopting it. Google an algorithm and odds are you'll find a sample in C++, basically the entire internet is your object exchange. For example GRBL is an open source G code interpreter that runs on the AVR, with gcc you could probably port it to the propeller, but with Spin it would be a re-write.
I think of C++ as a superset of C with additional features.
But, as microcontrollers become more powerful it will make more sense to use C++...
Except in certain circumstances, C and C++ are not interpreted languages as Spin is, so speed is going to be different. In C/C++, and limiting the discussion to just microcontrollers, your code is compiled into assembly language for the processor. (There are some exceptions to this, such as .net-powered devices, where the controller contains a runtime.)
C++ provides standard methods of implementing objects. Spin provides for this, too, though the syntax is necessarily different in order to handle things like loading the object into a new cog.
As noted by the others, probably the biggest "advantage" of C/C++ is that is so widely used, and the legion of coders is in the millions worldwide. It allows an easier shift between working with microcontrollers, mobile devices, personal computers, and so on.
-- Gordon
C is procedural and does not really have good ways to organize code. Spin gets kudos for it's objects by comparison. C can be forced into object mode because it has pointers, but that ain't very pretty.
C++ standardizes "C objects" in a reasonable way and adds a whole lot of extra stuff that you only really need for large system scale-ability. There are many C++ advantages that are usually leveraged only in certain environments. The inheritance feature Eric Ball mentions is probably one of the best general language features that comes from C++ (the worst is, well, debatable).
Not all of C++ features need to be used for an effective program. It is very comfortable implementing the limited but still powerful ideas of Spin. Eric's (ersmith) Spin to C++ translator demonstrates this nicely.
C# is a nice alternative to C++, but it is limited to certain platforms.
@Gordon, in Propeller-GCC we can start a COG several ways. One will be very familiar: cognew(pasm, parameters);
I think you confuse "better" with "is as Turing complete as". Orthogonality, expressiveness, abstraction, referential transparency, declarativeness all count for nothing then? Or was the smiley implied?
As you say all those things are important, and I would certainly prefer C++ or Spin over this Turing complete esoteric programming language:
I was trying to point out is that the advantage some languages enjoy (e.g. C++) over other languages (e.g. Spin) are less about what the language can achieve, and more about the programming ecosystem the languages exist within.
I found C++ to be useful where a larger part of the heavy lifting is done for you. Specifically, the STL and widget libraries such as QT. I would avoid programming a UI in anything less than a widget library like QT. Similarly, I had the opportunity to write a network monitoring client in C++, the use of the STL and streams was very valuable and made the code much smaller and much less bug prone.
On the other hand, I wrote a high performance distributed mail server in C and found the last ugly (logic) bug 10 years after it was in production, having delivered uncounted messages.
For me, expressing what I want to do and doing it without unnecessary complexity is paramount. Unnecessary complexity breeds bugs and leads to code that is difficult to understand and maintain.
That said, JavaScript has evolved to a point where it's almost a sport to write unreadable code (see jQuery).
For the BF impaired ....
My PASM interpreter thread starts here: http://forums.parallax.com/showthread.php?117194-Urban-M%FCller-s-Brainf***-Language-on-Propeller&p=852146&viewfull=1#post852146
There are actually 2 interpreters. Here's one written in Spin: http://forums.parallax.com/showthread.php?130416-PROPBrainFsck-an-Interpreter-for-Obfuscated-Programming.-Code-included.&p=985278&viewfull=1#post985278
C and its derivatives at intended to be a "portable assembler" that is, the allow a person the can write in C to write code on any processor that has C compiler support. Also, one may take advantage of extensive libraries of function that are already written in C, C++, etc (Did I get that right?) "C" really means "a way to get assembler type executables with having to hand code everything in assembler" which is really great.
The item that can occasionally cause issues is that all processor families are different to some degree or other. This means that not all library functions can run directly on given processor, without some modification and/or clear understanding of what those differences are. The C guys are going to great lengths to get the C material to run as one would expect on the prop. Sometimes this is a challenge as the prop is so very different in some areas, like interrupts. (the prop uses an entire cog instead of an interupt, etc).
The bottom line is eventually, we have to know the detail of how the prop works in order to use it to its fullest. The C guys are doing a lot of "packaging" that work for us, but as some point we have to start understand what's what and why.
There are other ways to get very fast execution speed, and use the processor to its fullest. One is writing directly in assembler, which takes years or decades to master. Other tools exist, but they tend to be consider "fringe" by folks that have a different preference.
If you learn C/C++ then you will be able to write code for various microprocessors not only the prop.
Also code written in C/C++ for other microprocessors could be used (with some modification) in a prop project.
Whilst spin has its place go for C/C++.
Just a thought!
I think this is a sound approach. I always do things in SPIN first if possible and then rewrite in assembly...
How much better then to have something like Spin as your pseudo code which you can actually run and test.
The motorcycle is the propeller.
Spin is the training wheels.
@prof briano (missed you at the chibots meeting yesterday) It is true that C was intended to be and still is a very low level language a very small step above assembly. While working at Lucent I "snuck <SP?> into a lecture given by Dennis Ritchie and he referred to C as a high level assembler. I have to admit that I never questioned the comment.
If you write a C program that doesn't make use of any library functions and compile it into assembly how efficient C compilers can be! There's a reason why the very vast majority of "embedded" machine control code is written in C!!
There's a move towards C++ for machine control but I have to admit to not understanding why.
Late to return to the party---
This is exactly the method to use. Doing this in a forth environment is particularly fast and easy, once one is familiar enough with the processor architecture and forth.
While C/C++ are the "lingua franca" of C/C++ programmers, do not fall for the line that they are the "only" or the "best" solution. These are simply a couple screw drivers in the tool box, along with the wrenches and files etc. Use a screw drive when you need a screw driver, but DON'T use a hammer when you need a wrench.