New code in C? Where and why, versus C++?
-
I for one have written C code in C++ compilers from Day One of proper programming just saved them as C files, I have never seen or felt the need to do C++, from the day I wrote a program in Assembler, same in C and again in C++, C & Assembler can't really remember around the same size, C++ three times the size. Okay today better optimisation but still I always get the feeling with C++ theres more going on than I know about. (I do mostly embedded programming)
A recurring misconception I see about C++ appears to be the notion that it will write code that you didn't tell it to write, or use memory you didn't tell it to use. It's simply not the case. More the issue is, knowing what you're telling the C++ compiler to generate in terms of code. To wit, there is no code that can be written in C that I cannot write in C++ and generate the equivalent machine instructions - even if doing so means giving up certain niceties like virtual inheritance, exceptions or run time type information. RAII is reason alone to consider minimalistic C++ as an alternative to straight C. It generates no extra code, and it reduces lots of bugs.
Real programmers use butterflies
-
This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?
Real programmers use butterflies
honey the codewitch wrote:
What is the purpose of writing new code in C for targeting, well ... anything?
You should use C because you are a witch, and love casting. :rolleyes:
The Science of King David's Court | Object Oriented Programming with C++
-
If you work in a team with young and eager programmers that completed their degree less than five years ago, there is one good reason: These young geniuses usually love to play around with every single feature of the tools they are given. They like to experiment, and they love to prove that they truly master even the most obscure little detail of the language, the library, the VCS or whatever. So they strive to use all of it, in the most intricate ways that "impresses" even the old coding gurus. C++ is not the language you would want to put in the hands of a young genius for proving that he can do something that no one else can do. You certainly can do dirty tricks in C as well, yet it is far more transparent than the "advanced" C++ features. (Besides, dirty C tricks often require that the programmer is very familiar with hardware and compiler operations, which is not as common with young programmers today, compared to thirty years ago). So C is a better way to discipline young geniuses, by depriving them of tools to create incomprehensive code. (Similar arguments can be used for staying away from git, or Docker orchestration, or CMake, or...)
Yeah. Though I might argue that an appropriate LINTing adjusted for dev-house practices upon check-in solves this and numerous other issues. Some developer education is necessary, but unless you can convincingly argue to me that a C developer is cheaper than a C++ developer, I'd just as soon hire the C++ developer, give them the set of practices, and LINT their code. YMMV
Real programmers use butterflies
-
honey the codewitch wrote:
What is the purpose of writing new code in C for targeting, well ... anything?
You should use C because you are a witch, and love casting. :rolleyes:
The Science of King David's Court | Object Oriented Programming with C++
I use Cisms like casting in C++ because I am a witch and use C++ in places that weren't really meant for full fidelity C++.
Real programmers use butterflies
-
I use Cisms like casting in C++ because I am a witch and use C++ in places that weren't really meant for full fidelity C++.
Real programmers use butterflies
I was being facetious, because in C I believe you are often forced to cast where you don't in C++. IE, from one of my articles...
SwcTabCtrl* tabControl = (SwcTabCtrl*) tabsC[selectedTabC];
in C, whereas C++ is:
SwcTabCtrl * tabControl = tabsC[selectedTabC];
I don't remember the reason for the forcing of the cast, but I believe it is some C-ism. Anyway, I detest the first code with great detestation. You shouldn't, though, because you are a witch!
The Science of King David's Court | Object Oriented Programming with C++
-
Mike Hankey wrote:
C was so efficient, faster and used less memory.
The last time someone told me that I challenged them to write code in C I couldn't write in C++. I never got a response.
Real programmers use butterflies
Exactly, it's like going from B&W to Color. Yeah I'm old enough to remember. :)
The less you need, the more you have. JaxCoder.com
-
How did I forget about "final"? I would have guessed it was newer than C++11 anyway - more like 17 or something. My hero! I guess I've got some code to refactor. Win!
Real programmers use butterflies
-
It is very easy to delete unwanted aspects of classes like copy constructors and copy operators. If you can reduce the amount of dynamic objects used, performance can really improve and deleting those things can help. I have seen this many times.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
-
Yes, I know and as I wrote they can be deleted or, in other words, effectively removed from usage. I do this to make a class uncopyable :
#define MakeUncopyable( className ) \
className( const className & ) = delete; \
className & operator=( const className & ) = delete;and any attempts to copy the object will fail to compile. This is useful for singleton objects and others where there can be only one.
"They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"
-
This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?
Real programmers use butterflies
Yes. I agree. To not mention that with the "placement new" operator is possible to create objects on existing memory blocks. Turning off the exception mechanism C++ is very good for the development of real-time embedded applications. Recreating the polymorfism in C, at best, has the same speed as in C++. But most of the time, C++ beats C in many areas, as the latter does not have templates. It's only possible to emulate them with macros, but macros, most of the time, are ugly and dangerous. Templates help with performances: for example, qsort has to do a callback to compare items; so, it spends CPU cycles, fools the CPU branch predictor, and possibly invalidates the level-1 instruction cache. With std::sort (or std::stable_sort) it's possible "to inject" the comparison into the algorithm. Yes, templates could make code bloating, but judicious use of non-template base classes could mitigate it.
-
honey the codewitch wrote:
What is the purpose of writing new code in C for targeting, well ... anything?
For products that you want to put on market, you have to comply to a lot of standards (like MISRA) that are much easier to implement in C, which is the state-of-the-art language. Given the amount of legacy code, this will probably not change in the next decade, so C remains the language if choice because, well, it was the language of choice. Of course, footprint & co also play a role, but not that much with modern compilers. Honestly, I am working in the automotive industry, and when I see what people are able to do with C, I am glad they are not given more powerful tools to work with ; I have my personal blacklist of vehicles I will never put a foot in, unless their code gets rewritten and their hardware redesigned.
-
A recurring misconception I see about C++ appears to be the notion that it will write code that you didn't tell it to write, or use memory you didn't tell it to use. It's simply not the case. More the issue is, knowing what you're telling the C++ compiler to generate in terms of code. To wit, there is no code that can be written in C that I cannot write in C++ and generate the equivalent machine instructions - even if doing so means giving up certain niceties like virtual inheritance, exceptions or run time type information. RAII is reason alone to consider minimalistic C++ as an alternative to straight C. It generates no extra code, and it reduces lots of bugs.
Real programmers use butterflies
-
I ran into this problem with the small footprint Arduinos, folks where asking mw why I wanted to use C++ when C was so efficient, faster and used less memory. But I found there really wasn't much difference in size or speed...no response from the peanut gallery. Now with devices with more memory I think that people still use C because that is what they are comfortable with. In the late 80s early 90s I worked for the railroad in the cyber division and I tried to get people to cross over to C++ but not one person could I get to change. I taught classes in using the development tools, debuggers and worked with them one on one...nope!
The less you need, the more you have. JaxCoder.com
i like this - "The less you need, the more you have"
-
This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?
Real programmers use butterflies
i would use C as a personal preference. it's subjective. also i rather be in company of C programmers than C++, that is also one reason you would use a particular language. but, i assume you posted a question to get some objective info as why C is a better choice than C++ in your use case scenario. maybe it is not. C++ was designed by Bjarne with zero cost abstractions in mind - "What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better." lots of people in the game making business use C style code with the C++ compiler or as little as possible classes, shallow hierarchies and such. the C++ compiler is a powerful beast and we (the programmers) are in the business of automation. so in that sense it's a better choice. C++ does not force you to use it in any way, that's good. i think Bjarne was hot on the OOP subject, if you read early editions of the book, but not so any more. he merely says C++ gives you complete support for OOP if you want it, among other things. here is a statement from Bjarne's book that you may consider when programming embedded - "Except for the new, delete, typeid, dynamic_cast, throw operators and the try block, individual C++ expressions and statements need no run-time support." for myself, i don't dislike OOP as a technical solution and capability. i dislike object orientation in people. that philosophy, look everything is an "object" and everything is related in hierarchies, took a wrong turn. instead of a solution for a problem it became a problem of it's own and a philosophy of life. solving the problem of say input data > processing > output data with classes and objects (if you like) became looking at classes as something that has a life of it's own. carried away by work on the class rather then on the whole... you know what i mean, that thing: this is a car and a car is a vehicle. then in the boom of OOP it became, either you do it our way or the highway.
-
Maybe I just vastly misunderstand C++, but AFAIK you don't have to use ctors, which are basically "operator overloads" in a sense for object "creation/instantiation/initatialization". If they don't do anything the compiler doesn't generate code for them. And addressof & does the same thing in C as it does in C++, unless you overload it. The theme here is, if you don't use the feature, it doesn't produce the code to run the feature. So if anything it seems a matter of education? Knowing what C++ will generate and what it won't. I write code for real time devices in C++ all the time. There's no more latency than C. Consider the following:
class A {
public:
int x;
};This generates the same exact code as
struct A {
int x;
};There are no constructors there. x will not get initialized on instantiation because I didn't write code for it. They are also both sizeof(int) in size. If i needed initialization
class A {
public:
int x;
A() : x(0) {}
// i'm omitting the big-5 here
};this is no different than if i needed initialization in C except it's cleaner:
struct A {
int x;
};
void initializeA(struct A *a) {
// can't remember if -> is strictly C++ or not
(*a).x=0;
}The former doesn't pollute the namespace, and also prevents the case of forgetting to call the initialization function.
Real programmers use butterflies
honey the codewitch wrote:
So if anything it seems a matter of education? Knowing what C++ will generate and what it won't.
Perhaps this is the biggest issue. The level of knowledge one has to earn to know how to make the compiler generate what they want and even more importantly, know what the compiler will generate when reading someone else's code. This knowledge has a cost (potentially a high cost) that needs to be maintained by the organization through the expense of time and paid experience of every developer reading or maintaining the code. That expense must be offset by the reduction in cost of using C++ features that eliminate writing boilerplate code and organizing code. I suspect with the far simpler rules/capabilities of C code, you have to look at a lot less code you didn't write to understand exactly what it is doing without resorting to using a debugger or looking at assembly. My current opinion is that the decision of C versus C++ would come down to the project size and the pool and expense of developers you want maintaining the code. Imagine a "C++ developer" jumping into a project that required diagnosing a set of code that used partial and full template specialization a few levels deep and being asked to refactor it. Could they do it? The statements "I know C++" and "I know C" carry two different levels of trust. I would trust most people who claim to know C and I can easily test their knowledge. With C++, the question for me becomes, what parts of C++ do you know and how well do you know each?
Dave B
-
This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?
Real programmers use butterflies
Who needs inheritance in embedded world?
Behzad
-
This is something I'm really curious about, and maybe it's just because I'm newish to coding small gadgets, though most of it is comfortable to me since I cut my teeth on 6502 CPUs back in the day. What is the purpose of writing new code in C for targeting, well ... anything? I've put away my C compiler and simply relied on limited use of C++ features to keep the footprint the same as it is in C: 1. no vtables. slightly complicates inheritance, as you can no longer do virtual ~foo() {} 2. extremely limited use of the STL, if at all. I typically use std::atomic if available, but not std::string, for example 3. judicious use of extern "C" when exporting my main, or importing "C" headers 4. no exceptions/SEH/etc, RTTI or other C++ frills that bloat code or stack Why do I do it? Primarily for that sweet sweet RAII, but also general source management. Classes can keep everything together. HPP based libraries are useful for simplifying distribution. public and private members replace the need for hiding in C files in many cases. and many other reasons. Should I not be doing it? Is there a reason this is bad form? Is there a reason you wouldn't?
Real programmers use butterflies
This is absolutely what I do. And I agree with you that so long as you understand and are happy with the 'hidden' code that C++ abstractions introduce, there's nothing wrong with using them. The only time I'll use C rather than C++ is if I'm targeting a platform that doesn't have a C++ compiler. And yes, I have one of those - it's a proprietary processor, has a port of GCC, Gnat (the GCC Ada compiler) but not G++. Yes, it's for a safety critical application. No, I can't talk any more about it. C++17 and 20 have introduced some nice new little abstractions, like `std::string_view` and `std::span` that would be useful in a C-ish world - basically, they're just pointer and length in a struct... Also - things like lambdas, once you realise they're just a function pointer (if you have no captures) or a function object, are equally usable in a resource constrained environment. The main thing I'd be wary of would be over many templates, in case they introduce too much code with multiple instantiations of functions for different template parameters.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
This is absolutely what I do. And I agree with you that so long as you understand and are happy with the 'hidden' code that C++ abstractions introduce, there's nothing wrong with using them. The only time I'll use C rather than C++ is if I'm targeting a platform that doesn't have a C++ compiler. And yes, I have one of those - it's a proprietary processor, has a port of GCC, Gnat (the GCC Ada compiler) but not G++. Yes, it's for a safety critical application. No, I can't talk any more about it. C++17 and 20 have introduced some nice new little abstractions, like `std::string_view` and `std::span` that would be useful in a C-ish world - basically, they're just pointer and length in a struct... Also - things like lambdas, once you realise they're just a function pointer (if you have no captures) or a function object, are equally usable in a resource constrained environment. The main thing I'd be wary of would be over many templates, in case they introduce too much code with multiple instantiations of functions for different template parameters.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
I didn't know lambdas reduce to a function pointer without captures. I thought they were always functors. Cool. On that subject, how would you go about accepting a lambda as a parameter to a function without accepting std::function? (i'm pretty sure you'd need to make the accepting function a template but it would be cool if you didn't) For the most part, in embedded code I limit my templates to things that basically replace preprocessor macros, like
#define BAR_STATIC_ALLOCATION_SIZE 256
struct bar {
char foo[BAR_STATIC_ALLOCATION_SIZE];
};instead i might make a declaration that allows for the following
bar<256> b;
Real programmers use butterflies
-
Who needs inheritance in embedded world?
Behzad
Why wouldn't you use inheritance? Do design patterns really change that significantly in embedded code?
Real programmers use butterflies
-
i would use C as a personal preference. it's subjective. also i rather be in company of C programmers than C++, that is also one reason you would use a particular language. but, i assume you posted a question to get some objective info as why C is a better choice than C++ in your use case scenario. maybe it is not. C++ was designed by Bjarne with zero cost abstractions in mind - "What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better." lots of people in the game making business use C style code with the C++ compiler or as little as possible classes, shallow hierarchies and such. the C++ compiler is a powerful beast and we (the programmers) are in the business of automation. so in that sense it's a better choice. C++ does not force you to use it in any way, that's good. i think Bjarne was hot on the OOP subject, if you read early editions of the book, but not so any more. he merely says C++ gives you complete support for OOP if you want it, among other things. here is a statement from Bjarne's book that you may consider when programming embedded - "Except for the new, delete, typeid, dynamic_cast, throw operators and the try block, individual C++ expressions and statements need no run-time support." for myself, i don't dislike OOP as a technical solution and capability. i dislike object orientation in people. that philosophy, look everything is an "object" and everything is related in hierarchies, took a wrong turn. instead of a solution for a problem it became a problem of it's own and a philosophy of life. solving the problem of say input data > processing > output data with classes and objects (if you like) became looking at classes as something that has a life of it's own. carried away by work on the class rather then on the whole... you know what i mean, that thing: this is a car and a car is a vehicle. then in the boom of OOP it became, either you do it our way or the highway.
I don't look at C++ as C with objects. If anything I look at it as C with templates. Classes are just fancy structs anyway, and overuse of OO is for the birds, although admittedly I use more OOP than Generic Programming in C++ when I'm coding embedded, mostly because of code bloat issues with GP.
Martin ISDN wrote:
everything is an "object" and everything is related in hierarchies, took a wrong turn.
I have a problem with those people too - particularly if they're calling themselves C++ developers. If you want an object oriented language, learn C#. That's not what C++ is about. You *can* use it that way, but I can also use my C compiler as an assembler by wrapping everything with asm {}.. it doesn't mean it's a great idea.
Real programmers use butterflies