5 ms·
C is king because the industry is currently dominated by people who have been doing this since before C++ was a thing. Additionally, most of these people are p
by Inityx 9y ago
C is king because the industry is currently dominated by people who have been doing this since before C++ was a thing.
Additionally, most of these people are primarily electrical engineers, and don't have as strong of a background in computer science. They've been using C for decades and it does everything they want, why would they take the time to learn the boundless complexity introduced by a language that offers them (what they perceive to be) very little?
Talking to long-time C programmers about C++ is actually surprisingly difficult: https://youtu.be/D7Sd8A6_fYU https://youtu.be/D7Sd8A6_fYU
- pknopf 9y agoLet me ask it this way: I'm a higher level programmer. Later in my career I happen to get down to microcontrollers, what would stop me from using C++?
- jwhite 9y agoNothing. I've worked on Cortex-M4 projects in C++. It's nice in many ways. The people working on the project had a much more diverse background than the typical EE who learned C as an undergrad mentioned in another thread.
- q3k 9y agoThe standard library (with all its' duplicate code resulting from hardcore templating) will blow up your flash space usage significantly, to the point where you will run out of it sooner than you expect. You will spend time finding alternative standard libraries that are size-optimized and you might end up rewriting a lot of what you take for granted in your C++ daily usage. For example, the Arduino environment is C++-based, but it's not anything like on the desktop due to it not shipping an std:: . Your typical heap-happy usage will not go down well on a microcontroller, either. Having very constrained RAM makes heap fragmentation much more of an issue.
- atilaneves 9y agoThen don't use the standard library. Don't even link it in. > Your typical heap-happy usage Huh? 1990s C++ was typically heap-happy, which is part of the reason Java looks the way it does. Idiomatic modern C++ uses the stack as much as possible. And one can use custom allocators.
- pjc50 9y agoNot a good fit for systems with small hardware stacks - PIC16 is still in use http://www.microcontrollerboard.com/pic_memory_organization.html http://www.microcontrollerboard.com/pic_memory_organization....
- dmitrygr 9y agoIt really depends how you use it. If you are approaching from the standpoint of "I am writing code on a microcontroller", which means no exceptions, no static initializers, probably no templates, definitely no rtti, then it will be all okay. If you approach it from the standpoint of "I'm just programming, how hard could it be? Let's just use std::", you're going to have a very bad time and very quickly. C++, when used in that way, tends to do a lot of things behind the scenes. This is perfectly okay in a place where you have an operating system and a linker that have your back. On an embedded system none of this is guaranteed.
- monocasa 9y agoStatic initializers and templates are great in a deeply embedded context, IMO.
- dmitrygr 9y agoFor you yes, since you know how they are implemented at linker level. But get a few junior devs on your team, and you'll be wondering why hundreds of thousands of cycles run before main is called, or why some driver code is being entered before it is initialized, since someone decided to make a static singleton object for some driver and called some driver method in the constructor which will run before main(), not realizing how this stuff really works underneath. So, C++ can be a wonderful tool in proper hands, but it is much easier to misuse than C in an embedded context.
- monocasa 9y agoI lead a team of six, including some junior devs. How initialization occurred (static or otherwise) was just something we made an explicit check off item in code review.
- dmitrygr 9y agoAwesome. Glad it worked out for you. I hope it stays so after you leave the team or no longer have time for each code review.
- shakna 9y agoA lot of people do. But they may as well not. They tend to write C-style C++. Simply put, because you can't use the STL, or a lot of other C++ features, or only with a lot of consideration. A whole swathe of the embedded world still cares about program size in bytes. There are some that don't, but they tend to be using Linux, and are at a higher abstraction level than many others in the industry. (Industry is kinda divided in half. Those who use tiny Linux machines, and those working with microcontrollers. It's a generalization, but generally fits.) The stuff I work on day-to-day, usually has between 1-4kb for dynamic memory, and 8-16kb for the compiled program. That line is also usually a bit blurry, and you can move things between both at runtime, but at various costs. With C++, you get tempted to use stuff like vector, which can blow your memory stack. I generally work with C++, but it looks like C. I get a few things like implicit pointers, for free, but generally still have to end up making most things explicit. But, unlike twenty years ago, I no longer have to dive into assembly unless the project is pushing it's limits. The compiler tends to be "good enough".
- Animats 9y agoNot much. The Arduino programming environment is really gcc in C++ mode, but a different library.
- bb88 9y ago> C is king because the industry is currently dominated by people who have been doing this since before C++ was a thing. No. If C++ were a great language those C coders would have moved over in an instant. One of the advantages of looking at C code is that you can actually figure out in your head what the assembly will look like.
- pknopf 9y agoAh, I forgot about the assembly. Is assembly important in microcontrollers?
- torn_pages 9y agoI would say it does. I learned microcontroller programming in assembly before I learned about C.
- viraptor 9y agoSometimes. Usually you just write "normal" C, until you realise your single `sprintf` use took 20% of your ROM size. Or until you need some interrupt handler to take no more than N cycles. You probably don't switch to assembly at that point, but you definitely start checking what the compiler output is and where are the bytes/cycles wasted. Actually writing assembly is more of a last resort time.
- tluyben2 9y agoBecause of cost we use very constrained microcontrollers; every byte literally counts. In the end it really matters cost wise (in mass production embedded every cent counts as well; using a high spec mcu just costs more) but we had to rewrite from C to assembly to get a few more kb for features in the flash. C++ or Rust are generally not good for the cost of materials.
- pjc50 9y agoWriting assembly tends to be restricted to the bits where you need it - special function prologues for interrupt handlers, requiring a particular unusual instruction for something, time-sensitive or cycle-accurate "beam racing" code. Reading assembly is more useful, especially if your platform's debugger isn't very good.