Anonymous
Hello brothers and sisters. I had started with C language for sometime and a friend of mine said that I go to C++ with a reason that C language is outdated So I have come to my comrades to help What should I go with?
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
Wdym by vanishing this simple language?
C++ is a superset of C If you want to do the C part, do it inside C++
MᏫᎻᎯᎷᎷᎬᎠ
We already can call already-existing C functions inside a C++ code
olli
Why it's still popular?! And what C++ can do to deminish this popularity to zero
C is still used for kernels and firmware, and can also be found in drivers, databases and many more. Especially for safety-related systems C is in many cases still the language of choice because there are existing verified compilers one can rely on; this is not the case for C++ and I don't see a "safe" C++ compiler being released anytime soon. If you only rely on the C subset in C++, why use C++? Your toolchains will be heavier and build times increase significantly. Even if you never write production C code, I think it's still worth learning. Just try to implement polymorphism in C and it will teach you a lot about how programming languages and computer systems work. Knowing C also helps understanding how vulnerabilities/exploits/attacks/.. work.
Anonymous
What do you mean by safe compiler?! Yeah, C++ is like a grammar that ends up in C code in the binary file, just with more constraints like encapsulation and other OOP rules and not to forget about the template instantiations and so on, they are all done by the compiler, but ends up to a C code with some name mangling to preserve the uniquness of entities, but that's all good, right?!
A compiler that flags uses of exceptions, RTTI, multiple inheritance, templates (maybe not), virtual base classes etc as errors so that such code is not allowed in embedded platforms. In other words, a C++ compiler much simpler than the ones existing today thus making it more safer. A standard working committee dedicated to addressing this issue is lacking and so it is left to the individual developer's discretion to avoid these parts of C++ when working in resource constrained environments. Given that a vast majority of C++ is not suitable for such platforms, it doesnt seem like C is a bad choice for such platforms.
olli
What do you mean by safe compiler?! Yeah, C++ is like a grammar that ends up in C code in the binary file, just with more constraints like encapsulation and other OOP rules and not to forget about the template instantiations and so on, they are all done by the compiler, but ends up to a C code with some name mangling to preserve the uniquness of entities, but that's all good, right?!
To write safe software IEC 61508 defines SILs (Safety integrity level) and to create such a software your whole code and toolchain must adhere to these requirements. These are used e.g. in aviation, medical, automotive and space industries to minimize the probability of a failure. A C++ compiler is likely to have more bugs and a higher failure rate than a C compiler.
Anonymous
I've always wondered why such uses are forbidden in the system/embedded programming world? Don't have a background about it + You can also disable the exception part RTTI(I think)
Because they are not efficient in the embedded world and they lead to complication both in compiler development and code generation. If you have a liking towards C++ because of its support for OOPS and avoid the above mentioned parts, you can still do embedded development in C++.
olli
And I've thought 35+ years of existence develops maturity of both compiler and the language
But I don't think the 35 years make sense here, because C++11 took developer years to fix ABI breaking changes - C++ just changes a lot in comparison to C.
olli
Let alone the C++20 changes, getting coroutines into the standard took years...
MᏫᎻᎯᎷᎷᎬᎠ
olli
Is "ABI breaking" a reason for why not to use C++ over C in the above mentioned safe softwares?
not necessarily, my point was rather that C++ changes way quicker and in more impactful ways than C does.
MᏫᎻᎯᎷᎷᎬᎠ
Yeah We need Epochs in the standard, really, all of this because of lack of that
MᏫᎻᎯᎷᎷᎬᎠ
Just look at Rust edition
olli
C++ is a beast with so flexible rules You can adjust just about anything You can remove the code duplication if you stop abusing templates
I guess it's always a cost-benefit analysis. Sure you can use only a small subset, but is it worth it? A lot of embedded projects use their own code generation tools, apart from RAII I'm not missing many C++ features.
olli
but Rust is way simpler than C++. There can be C++ bugs that are caused by the order of member declarations in a class., something that can not happen in C.
olli
MᏫᎻᎯᎷᎷᎬᎠ
Depending on the project, I don't. Which tweaks do you need?
Does C has constexpr everything? Not sure really
olli
Does C has constexpr everything? Not sure really
and why do you need it? Linux works fine without them. Sure, no doubt, there are nice, but do I need them?
Engineer
http://spinroot.com/cobra/
Engineer
http://spinroot.com/cobra/
Cobra is a structural source code analyzer, fast enough that it can be used interactively. The tool prototype (Version 1.0) was developed at NASA's Jet Propulsion Laboratory late 2015, and released for general distribution about a year later. Versions 2 and 3 of the tool are extended versions that can handle interactive analyses of code bases with up to millions of lines of code, while supporting a significantly richer online query scripting language. It also comes with multi-core support for many types of queries, including a new set of cyber-security related checks. Starting with Version 3, the Cobra code is distributed in open source form at github.com/nimble-code/Cobra. Cobra can analyze C, C++, Ada, and Python, and can relatively easily be retargeted for other languages. The distribution includes sample query libraries and scripts. Tutorial A comprehensive online tutorial and demo of Version 3.1 of Cobra is available at this link: Online Tutorial (about 165 minutes total, in 8 parts, with exercises). (The current Cobra version is 3.5, which has a few extensions, but should be backward compatible with 3.1.) If you just want to look at the demo, check this link: Demo (it's a little over 21 minutes). For bug reports and additional information: gholzmann atsign acm dot org
MᏫᎻᎯᎷᎷᎬᎠ
olli
Really?! C structS size and alignment don't change with the order of the the members?
sure they do. But the order destructors are called in does not.
Anonymous
And I've thought 35+ years of existence develops maturity of both compiler and the language
The language is changing a lot as well. Compare C++11 to C11 and C++17 to C18. C11 introduced atomics support and a memory model but not much. C18 is what many would consider a TS in C++ world. C++ changes a lot forcing the compilers to do so as well. Infact till 2002, there was a standard committee dedicated to making C++ a safe choice for mission critical platforms. But this group was disbanded after Bjarne Stoustrup's public rant against them. This group had advocated for compilers without the features I had listed above and they did have many MNCs supporting them but unfortunately didnt take off. Their website still exists : http://www.caravan.net/ec2plus/
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
but Rust is way simpler than C++. There can be C++ bugs that are caused by the order of member declarations in a class., something that can not happen in C.
+ as I said C++ can be much more simpler if we got Epochs in the standard I've talked to the proposer of the feature He said: "Epochs in the current form was rejected in Prague, but there's still a chance it could come back"
olli
yes, sure - it would be a nice feature, but we can't wait for it to land and need to use what we have.
olli
Getting something into the standard is not easy and on-top it needs to be implemented as well.
MᏫᎻᎯᎷᎷᎬᎠ
yes, sure - it would be a nice feature, but we can't wait for it to land and need to use what we have.
I think this is a philosophical C++ is doing great until now, with lots of systems on its shoulders, no problem with waiting in my opinion
olli
Well If you need more performance?😂😂 Am I missing something?
Good point. But in my experience embedded projects often used their own codegen tool that was part of the toolchain which code is not slower and still faster to build. And in general, safety > performance.
MᏫᎻᎯᎷᎷᎬᎠ
But I believe if we got modules and epochs Everything will move faster
MᏫᎻᎯᎷᎷᎬᎠ
Good point. But in my experience embedded projects often used their own codegen tool that was part of the toolchain which code is not slower and still faster to build. And in general, safety > performance.
Well You would depend on a standard rather than a specific compiler extension/implementation That if you mean compiler by the word "toolchain"
MᏫᎻᎯᎷᎷᎬᎠ
Or std::variant as a safe replacement for alot of dynamic polymorphism uses
olli
Well You would depend on a standard rather than a specific compiler extension/implementation That if you mean compiler by the word "toolchain"
Toolchain as in every tool involved, not only the compiler. In our case it was a simple tool that generated header files and C code, no extension to any compiler was made. And the code generated was pretty optimized. Also, don't underestimate compilers. They are clever whether you use constexpr or not
レッギ
Anyone have recommended good source for learning c++ at advanced ?
olli
For example: Member the order of evaluation that was standardized in C++17
regarding the member order, my point was not about it being standardized or not - such a bug is easy to miss and not trivial to find once something crashes.
olli
Does the C compiler reorder the struct data members?
the order of the members defines the order in which destructors are called. If a destructor has a side effect there might be a bug. Since C does not have destructors this problem is non-existent.
MᏫᎻᎯᎷᎷᎬᎠ
I'm not sure why it should reorder them Sometimes having more memory is faster because of the alignment, like int is faster than short in some architectures
olli
http://eel.is/c++draft/class.dtor#14 [...] Bases and members are destroyed in the reverse order of the completion of their constructor and http://eel.is/c++draft/class.base.init#13 In a non-delegating constructor, initialization proceeds in the following order: [...] - Then, non-static data members are initialized in the order they were declared in the class definition [...]
olli
This is more like a coupling issue between the data members
I agree, an issue that does not exist in C. That's pretty much my point. This issue is extremely hard to spot and people did not notice during code review.
olli
Well You can define a destructor for your class
not sure how that would help if the type has automatic storage duration
MᏫᎻᎯᎷᎷᎬᎠ
The point is: Like Bjarne said In C++ you got a shotgun within your hand that could blow you up, not just your feet But in the possiblity concerns, C++ can do all of what C can Like the mentioned problems was caused because the programmer is not an expert, npt because something in the language is not possible
MᏫᎻᎯᎷᎷᎬᎠ
not sure how that would help if the type has automatic storage duration
Since those members have coupling, that probably means they have to do some checking in order to provide a safe destruction. Like std::unique_ptr does a check for the raw pointer in it's destructor
MᏫᎻᎯᎷᎷᎬᎠ
and C can do all C++ can. Your point reminds me pretty much of why Linux is not written in C++.
But in a lot of handwritten way Like Using macros instead of templates
Anonymous
is there a java developer here
Anonymous
The point is: Like Bjarne said In C++ you got a shotgun within your hand that could blow you up, not just your feet But in the possiblity concerns, C++ can do all of what C can Like the mentioned problems was caused because the programmer is not an expert, npt because something in the language is not possible
The point is that C++ enforces certain things that goes contradictory to what the programmer intends in his written code. In C this is not really a problem. You mentioned reordering of data members in a struct in C. This is not done by a C compiler (I am not sure if something changed in C18) and doesnt contradict the programmer's intention in anyway except in rare cases where a programmer is playing around with bit fields and addresses.
Anonymous
If yes
olli
is there a java developer here
wrong group - please read the rules.
Anonymous
🆗
MᏫᎻᎯᎷᎷᎬᎠ
The RAII lack in C also makes it harder to just simple things You need to provide checks in probably every function call instead of just using a reference Or sometime you don't know when your function returns or what error can you get That makes harder to free the memory That can be solved by just a std::unique_ptr
olli
The RAII lack in C also makes it harder to just simple things You need to provide checks in probably every function call instead of just using a reference Or sometime you don't know when your function returns or what error can you get That makes harder to free the memory That can be solved by just a std::unique_ptr
I agree with the RAII part, hence I mentioned it earlier as the one feature I miss the most. Having a good coding style however simplifies things a lot. But it that's the only argument for C++ in an embedded system, I am fine without it.
olli
Honestly, I don't think C is that bad as many people make it. When writing C or C++ you need to know what you are doing anyway. Imho a good read about that https://floooh.github.io/2018/06/02/one-year-of-c.html
olli
in my opinion: long story short, use whatever makes sense for you. For the embedded/low-level projects I worked on I understood why C was chosen.
MᏫᎻᎯᎷᎷᎬᎠ
Yeah Nice discussion
Ammar
Yeah, nice discussion, I really enjoy reading it.
@𝑺𝒐𝒃𝒌𝒂
Anonymous
Nice discussion all of you.admin and others
Anonymous
Just look at Rust edition
rust is not abi stable
Anonymous
Soo I have gone with C++
Anonymous
Any one to send me what I will be reading
Anonymous
?