Rockbox.org home
Downloads
Release release
Dev builds dev builds
Extras extras
themes themes
Documentation
Manual manual
Wiki wiki
Device Status device status
Support
Forums forums
Mailing lists mailing lists
IRC IRC
Development
Bugs bugs
Patches patches
Dev Guide dev guide
Search



Donate

Rockbox Technical Forums


Login with username, password and session length
Home Help Search Staff List Login Register
News:

Welcome to the Rockbox Technical Forums!

+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Why C?
« previous next »
  • Print
Pages: 1 [2] 3

Author Topic: Why C?  (Read 12330 times)

Offline bascule

  • Rockbox Expert
  • Member
  • *
  • Posts: 1298
Re: Why C?
« Reply #15 on: December 04, 2007, 10:40:12 AM »
Quote from: zajacattack on December 04, 2007, 10:25:52 AM
Quote
C++ is not a good choice simply because several of us who started Rockbox just don't like C++
Well, I was curious to whether there was an actual reason other than "not liking" C++.

Well, yes there was:

Quote from: Bagder on December 04, 2007, 04:21:12 AM
...and the fact that lots and lots of the 3rd party libs we use are plain C and we use them as unmodified as possible.

FYI, some (performance-critical) parts of Rockbox are written in assembly. Now that's a proper programming language, none of these silly abstractions and high-level functions... ;)
Logged
DataBase fanboy and author of the totally overhauled Rockbox Sync Tool

Offline GodEater

  • Member
  • *
  • Posts: 2829
Re: Why C?
« Reply #16 on: December 04, 2007, 11:16:36 AM »
As well, a lot of C++ features require dynamically allocated memory - which of course Rockbox :

a) Doesn't do.
and
b) Will never do.
Logged

Read The Manual Please

zajacattack

  • Guest
Re: Why C?
« Reply #17 on: December 04, 2007, 05:01:05 PM »
OK, I see now. So, C++ is better for high-power computers, but not efficient enough for MP3 players, right? Also, I know some is written in assembly, as that is the most efficient code (exact control over processor, smallest executables, etc.). And, I read about the dynamic allocation of memory. So, I'm curious, why will rockbox never do that?
Logged

Offline AlexP

  • Global Moderator
  • Member
  • *
  • Posts: 3688
  • ex-BigBambi
Re: Why C?
« Reply #18 on: December 04, 2007, 05:03:33 PM »
http://www.rockbox.org/twiki/bin/view/Main/WebHome?topic=WhyNoMalloc
Logged
H140, F60, S120, e260, c240, Clip, Fuze v2, Connect, MP170, Meizu M3, Nano 1G, Android

Offline mnhnhyouh

  • Artist
  • Member
  • *
  • Posts: 333
Re: Why C?
« Reply #19 on: December 04, 2007, 07:29:42 PM »
In line with the assembly comments above, programming languages are just a convenient way of writing assembly, just more abstraction, so less lines to write and this makes complex code easier to understand for most programmers.

The compiler then turns the code written in other languages into the actual instructions that the cpu executes.

So in an ideal world C, C++, Java and all the other compilers would produce the same executable code. However this is not always the case....

h
Logged

Offline Llorean

  • Member
  • *
  • Posts: 12931
Re: Why C?
« Reply #20 on: December 04, 2007, 07:39:49 PM »
No, JAVA would never produce the same executable code, because java (when used as intended) isn't compiled to assembly, it's compiled to byte code which then an interpreter deals with and executes code, which is one of the primary reasons it's essentially physically impossible for java to be as efficient at C or C++.

Edit for technical correctness: java can be faster than C or C++, in some general use, because the interpreter can be pre-optimized in some ways. But a C or C++ program properly handled should always be able to perform faster than a JAVA program doing the same thing, if enough time and effort is spent, while ava has some advantages for the more general cases.

In an ideal world C and C++ would be create identical executable code when given identical algorithms, but then identical algorithms in C and C++ would probably *be* identical code. ;)

But yeah, the distinction between C and C++ is more that C, being a "simpler" language keeps the coder more explicit, while C++ lends one to abstractions that, while often making the code "easier" to read (I put such words in quotes, because they're very subjective) tends to leave the code less explicit and more generalized, as the intent with C++ is for the programmer to write in an "object oriented" manner.
« Last Edit: December 04, 2007, 08:12:26 PM by Llorean »
Logged

Offline mnhnhyouh

  • Artist
  • Member
  • *
  • Posts: 333
Re: Why C?
« Reply #21 on: December 04, 2007, 08:18:54 PM »
Quote from: Llorean on December 04, 2007, 07:39:49 PM
No, JAVA would never produce the same executable code, because java (when used as intended) isn't compiled to assembly, it's compiled to byte code which then an interpreter deals with and executes code,

But doesnt this ultimately have to be approximately the same code? (It has been 20 years since I studied programming in any formal sense.)

h

Logged

Offline safetydan

  • Developer
  • Member
  • *
  • Posts: 248
Re: Why C?
« Reply #22 on: December 04, 2007, 08:21:24 PM »
Well sort of, but Java takes the long way round to get there. C/C++ will be much closer to the final machine code representation. Assembly closer still.
Logged

zajacattack

  • Guest
Re: Why C?
« Reply #23 on: December 04, 2007, 08:25:07 PM »
So, the lower-level the code, generally, the closer to exact machine code right? The most efficient would be to program in machine code, but as this is pretty much impossible, the code ends up being, in order from most to least efficient:
(1) Assembly
(2) C
(3) C++
(4) Java
(5) C#
...

I am correct, right?
Logged

Offline cool_walking_

  • Rockbox Expert
  • Member
  • *
  • Posts: 695
Re: Why C?
« Reply #24 on: December 04, 2007, 08:31:02 PM »
Well it also depends on the compiler. If you had a real crappy C compiler, and an awesome Java compiler, the Java code would run faster.

You could write something in assembly badly, or write something in C, that the compiler optimises very well.
Logged

Offline Llorean

  • Member
  • *
  • Posts: 12931
Re: Why C?
« Reply #25 on: December 04, 2007, 08:31:14 PM »
It's more like there are tiers.

Machine Code / Assembly leave you in more or less exact control of what the processor does.

Compiled languages that are compiled directly to executable code like C / C++ / Fortran and others are turned into machine code, so you're pretty close to direct control, but have to depend on the compiler to make the right choices during conversion. The smarter the compiler, the better.

Then comes languages compiled into bytecode like C# and java. These languages are sort of a halfway point between the last tier and what will be the next tier. Their nature allows them to be optimized in some ways that allow them to be much faster than a purely interpreted language, in general, while at the same time keeping their compiled form from being specific to one hardware. This means if you have a java program it should run on any computer with the appropriate version java installed, independent of where it was compiled originally, for example.

Then comes interpreted programs. These are generally the least efficient. They're never directly turned into machine code. Instead essentially a program reads them line by line, and then goes and does what that line said, then comes back and gets the next line it's supposed to get, and does that. This does mean though that in testing you don't have to recompile every time, you just have to make your changes and start running again. These are also sometimes referred to as "scripting languages" though there can be subtle distinctions that people will make, and I won't go into here.

Generally speaking the further you move down that list, the less direct control you have over what's done, and the more you have to trust in someone else for your optimizations.
Logged

Offline GodEater

  • Member
  • *
  • Posts: 2829
Re: Why C?
« Reply #26 on: December 05, 2007, 02:43:34 AM »
Quote from: Llorean on December 04, 2007, 08:31:14 PM
Then comes interpreted programs. These are generally the least efficient. They're never directly turned into machine code.

Except at runtime. Otherwise they wouldn't work at all. Computers don't run anything that isn't machine code.

They get parsed turned into machine code all at run time, hence they're slower than things like Java and C#, which get parsed and turned into bytecode once when you "compile" them. Then the bytecode is turned directly into machine code by the virtual machine which runs the program.

Logged

Read The Manual Please

Offline MaW

  • Member
  • *
  • Posts: 11
Re: Why C?
« Reply #27 on: December 18, 2007, 09:39:19 AM »
Well technically speaking the Java->bytecode or C#->bytecode phase is indeed compilation, it's just that it doesn't target the language which is spoken by the processor on the system which the program is intended to run on. This bytecode is then either interpreted at runtime, or compiled down to machine code just before runtime by a JIT compiler (or sometimes in advance, if you want to save some startup time and don't need the distributables to be portable). Ultimately, if you've got a good VM and a good JIT, you can get something close enough to the performance possible with a lower-level language to suit ordinary desktop applications very well, and that efficiency loss is usually acceptable in exchange for the programmer productivity boost from using a modern, well-designed high-level programming language.

I'm obviously not referring to Java when I say 'well-designed' though.

C is a good language. You can do pretty much anything you want in it, and you can do a lot of things well (although not necessarily easily). A superb choice for programming something like Rockbox though. How can you write the kernel of an operating system in a language which requires a virtual machine to run? What would the VM run on? Being close to the hardware there really really helps.

I wouldn't dismiss C++ myself, but that's probably because I'm a C++ programmer. Rockbox's decision not to provide malloc() rather nukes most of C++'s easy features though. In an environment which does have full memory management, it's quite capable of being just as fast and lean as C. You do have to be careful with it of course.

Ultimately though, Rockbox just had to be written in C. Multiple architectures (rules out using all assembler, along with an assumed desire from the developers to preserve their remaining hair), tight memory limits, tight hardware limits... if you're not in a reasonably low-level language, you just don't have enough control to pull it off.

Probably best to ignore all that though. I don't develop against Rockbox at the moment, and I may well never start.
Logged

Offline AlexP

  • Global Moderator
  • Member
  • *
  • Posts: 3688
  • ex-BigBambi
Re: Why C?
« Reply #28 on: December 19, 2007, 10:11:24 PM »
Quote from: MaW on December 18, 2007, 09:39:19 AM
Probably best to ignore all that though. I don't develop against Rockbox at the moment, and I may well never start.

I don't know, seems a pretty good explanation to me :)
Logged
H140, F60, S120, e260, c240, Clip, Fuze v2, Connect, MP170, Meizu M3, Nano 1G, Android

Offline safetydan

  • Developer
  • Member
  • *
  • Posts: 248
Re: Why C?
« Reply #29 on: December 19, 2007, 10:39:25 PM »
I'd object to the comment about Java not being well designed, but that's getting really off topic  ;)
Logged

  • Print
Pages: 1 [2] 3
« previous next »
+  Rockbox Technical Forums
|-+  Rockbox Development
| |-+  Starting Development and Compiling
| | |-+  Why C?
 

  • SMF 2.0.18 | SMF © 2021, Simple Machines
  • Rockbox Privacy Policy
  • XHTML
  • RSS
  • WAP2

Page created in 1.918 seconds with 20 queries.