简体   繁体   English

对于实时应用程序,哪个更好的C或C ++?

[英]For real-time application, which is better C or C++?

I'm electronic engineer with experience with both language C and C++ (I wrote with C for micro-controller and with C++ I wrote for Windows with Borland C++ Builder) 我是具有C语言和C ++经验的电子工程师(我用C编写微控制器,用C ++编写,用Borland C ++ Builder编写的Windows)

My company develops motor control products, and we are working with STM32 and IAR compiler. 我公司开发电机控制产品,我们正在使用STM32和IAR编译器。

I recognize the technical differences between the languages, I interest in the development coast and in the maintenance cost of the code: 我认识到语言之间的技术差异,我对开发海岸和代码的维护成本感兴趣:

  1. Is the development time of writing C++ code is longer then C? 编写C ++代码的开发时间是否比C长?
  2. Is the maintenance cost of C++ code is cheaper then C? C ++代码的维护成本是否比C便宜? (I know that always will be changes in the code) (我知道代码总会有变化)
  3. Is it easy to documents code in C++ against C? 用C语言编写针对C的代码是否容易? (Documents that describe how the code works) (描述代码如何工作的文档)

This is incredibly subjective. 这是非常主观的。 I personally think developing in C++ costs less in terms of development time and maintenance, especially for large, complex projects. 个人认为使用C ++进行开发在开发时间和维护方面的成本较低,特别是对于大型复杂项目。 This is because most large, complex C projects I've seen invariably end up implementing C++ features (such as polymorphism) one way or another. 这是因为我见过的大多数大型复杂C项目总是以这种或那种方式最终实现C ++特性(例如多态)。 Additionally, I would argue that certain C++ features such as namespaces, data encapsulation and automatic initialization/destruction of objects increase maintainability for large projects; 另外,我认为某些C ++特性,如命名空间,数据封装和对象的自动初始化/破坏,增加了大型项目的可维护性; although again some of those things can be more or less emulated to varying degrees of effectiveness in C. 虽然这些东西中的一些可以或多或少地被模仿到C中的不同程度的有效性。

But your mileage may vary considerably, depending on a ton of additional factors. 但是,根据大量的其他因素,您的里程可能会有很大差异。 For example, if your programming team has more experience with C than C++, then it may not be worth doing it in C++. 例如,如果你的编程团队在使用C语言方面比C ++有更多的经验,那么在C ++中可能不值得这样做。

Is the development time of writing C++ code is longer then C? 编写C ++代码的开发时间是否比C长?

This will depend entirely on your programmers. 这完全取决于您的程序员。 Are they better at writing C or C++? 他们擅长写C或C ++吗?

Is the maintenance cost of C++ code is cheaper then C? C ++代码的维护成本是否比C便宜? (I know that always will be changes in the code) (我知道代码总会有变化)

Again, will depend on your programmers. 再次,将取决于您的程序员。 You can write maintainable or unmaintainable code in any language. 您可以使用任何语言编写可维护或不可维护的代码。

Is it easy to documents code in C++ against C? 用C语言编写针对C的代码是否容易? (Documents that describe how the code works) (描述代码如何工作的文档)

Depends entirely on your team and the tools you are using, but I would say they would probably be about the same. 完全取决于您的团队和您正在使用的工具,但我想他们可能会大致相同。

So, to summarize, it all depends on the people you have and what they are best at. 总而言之,这一切都取决于你拥有的人和他们最擅长的人。 If you try to put a bunch of C programmers on a C++ project, you'll probably end up with some pretty bad C++. 如果你试图将一堆C程序员放在一个C ++项目上,你可能会得到一些非常糟糕的C ++。 Likewise if you try to put a bunch of C++ guys on a pure C project. 同样,如果你试图将一堆C ++人员放在纯C项目上。

  1. Depends on the experience of the team involved. 取决于所涉团队的经验。 With C++ you have access to a richer set of library functions (but be careful of their footprint when embedding them). 使用C ++,您可以访问更丰富的库函数(但在嵌入它们时要小心它们的足迹)。

  2. Well-designed C++ code will be easier to maintain than C code, as it allows for some syntactic sugar (constructors, destructors, RAII). 精心设计的C ++代码比C代码更容易维护,因为它允许一些语法糖(构造函数,析构函数,RAII)。

  3. The cost of documentation is about the same. 文件的成本大致相同。

Summary 摘要

If decent C++ developers and compilers are available, stick to C++. 如果可以使用体面的C ++开发人员和编译器,请坚持使用C ++。

If not, then you will have to balance the learning curve, the project complexity (which will be easied away by the language adaptability), and the available compilers. 如果没有,那么你将不得不平衡学习曲线,项目复杂性(将通过语言适应性缓解)和可用的编译器。

Learning curve 学习曲线

In C, the learning curve for the language is easier: Your developers will more easily grasp the fundamental features of the language... C++ is a much more larger language, containing both a C subset, and a template subset, and an object subset, etc., each subset quite different from the previous one. 在C语言中,语言的学习曲线更容易:您的开发人员将更容易掌握语言的基本特征... C ++是一种更大的语言,包含C子集,模板子集和对象子集等等,每个子集与前一个完全不同。

Fact is, it can be easy to have very inefficient code with C++. 事实上,用C ++编写代码效率很低很容易。 Not because "the language is slow", but because developers will sometimes code it the wrong way (usually by ignoring non-C parts of the C++ languages, like references, etc.). 不是因为“语言很慢”,而是因为开发人员有时会以错误的方式编写代码(通常忽略C ++语言的非C部分,如引用等)。 This is part of the "learning curve" problem. 这是“学习曲线”问题的一部分。

But once the "learning curve" is over, then we can look at the language features... 但是一旦“学习曲线”结束,我们就可以看一下语言特征......

Language adaptability 语言适应性

C is quite blunt. C很直率。 Either you use C's built-in constructs, and it will be easy (like, adding two integers...), or you don't, and it will be error prone, as well as potentially inefficient. 要么你使用C的内置结构,要么很容易(比如,添加两个整数......),要么你没有,并且它将容易出错,并且可能效率低下。

C++ makes it easy to avoid resource leaks, buffer overruns, memory or stack corruption. C ++可以轻松避免资源泄漏,缓冲区溢出,内存或堆栈损坏。 In C, those could happen almost at every line of code. 在C中,这几乎可以在每行代码中发生。

With inlining and encapsulation, C++ makes it easy to work efficiently and securely with any type (ie user-defined types). 通过内联和封装,C ++可以轻松高效,安全地使用任何类型(即用户定义的类型)。 With templates and OOP, C++ makes it very very easy to extend some type with an additional feature. 使用模板和OOP,C ++可以非常容易地扩展某些类型的附加功能。

Of course, all this assume C and C++ will have equally efficient compilers... 当然,所有这些都假设C和C ++将具有同样高效的编译器......

Compilers 编译器

C++ makes a lot of assumption on compilers. C ++在编译器上做了很多假设。 By reading an STL implementation, you'll see a lot of apparently useless function calls, and will wonder at the cost of all this "fancy". 通过阅读STL实现,您将看到许多明显无用的函数调用,并且会怀疑所有这些“花哨”的代价。 Fact is, the compiler will inline those away, making the resulting binary smaller that you thought possible. 事实上,编译器会将它们内联,使得生成的二进制文件更小,您认为可能。

But if your compiler is not able to do that, then perhaps C++ would be a bad idea. 但是如果你的编译器无法做到这一点,那么C ++可能是一个坏主意。

Conclusion 结论

1 - Is the development time of writing C++ code is longer then C? 1 - 编写C ++代码的开发时间是否比C长?

It depends on the size/complexity of the project, and on the familiarity of your developers with the chosen language. 这取决于项目的大小/复杂程度,以及开发人员对所选语言的熟悉程度。 If you have C++ developers, stick to C++ . 如果您有C ++开发人员,请坚持使用C ++

2 - Is the maintenance cost of C++ code is cheaper then C? 2 - C ++代码的维护成本是否比C便宜? (I know that always will be changes in the code) (我知道代码总会有变化)

Again, it depends on the size/complexity of the project. 同样,它取决于项目的规模/复杂程度。 The more complex it is, the more architecture you'll need, and thus, the more language supported features you'll need, and then, use C++. 它越复杂,您需要的架构就越多,因此,您需要的语言支持的功能就越多,然后使用C ++。

3 - Is it easy to documents code in C++ against C? 3 - 用C语言编写针对C的代码是否容易? (Documents that describe how the code works) (描述代码如何工作的文档)

I assume you are talking about code clarity (ie not Doxygen-like documentation, which works the same way for both C and C++). 我假设您正在谈论代码清晰度(即不是类似Doxygen的文档,它对C和C ++的工作方式相同)。

It depends on how much features you use. 这取决于您使用的功能。 For example, using a C++ string is quite natural, compared to the equivalent code in C. In fact, using complex structures is a lot more easier in C++ than in C... 例如,与C中的等效代码相比,使用C ++字符串是很自然的。实际上,使用复杂结构在C ++中比在C中更容易...

MyMatrix A, B, C ; // My Matrix is an user-defined object
// etc.
C = A * B ;        // If you believe this is anything but a
                   // multiplication of two matrices, then
                   // you need serious medical help.

There's no need to document this code. 没有必要记录此代码。 Everyone knows what a matrix is, and what a multiplication is. 每个人都知道矩阵是什么,以及乘法是什么。 The same code in C would have been quite more verbose... C中的相同代码会更加冗长......

But then, C++ can be quite verbose, too. 但是,C ++也可能非常冗长。 Without a foreach (like Boost.FOREACH), writing a loop to iterate each item in a STL container can be quite impressive : 没有foreach(比如Boost.FOREACH),编写一个循环来迭代STL容器中的每个项目可能会令人印象深刻:

for(std::map<int, std::string>::iterator it = myMap.begin(), itEnd = myMap.end() ;
    it != itEnd ;
    ++it)
{
   // Do something quite complicated...
}

Some C++ language features are even non-natural when coming from a procedural language, and will be part of the learning curve. 当来自过程语言时,一些C ++语言特征甚至是非自然的,并且将成为学习曲线的一部分。 And the messages resulting from a compiler error for this kind of code could break the sanity of your developers to pieces, if they are unprepared for it. 如果这些代码没有准备好,那么这种代码的编译器错误所产生的消息可能会破坏开发人员的理智。

:-) :-)

So it gets depends again on the knowledge of C++ among your developers. 所以它再次取决于你的开发人员对C ++的了解。

My personal conclusion? 我的个人结论?

I started as a C developer. 我最初是一名C开发人员。 Upon learning C++, I found the language too complicated for my uses, but still, I thought some C++ features would be cool in C... Since then, after years of experience, my viewpoint changed completely. 在学习C ++之后,我发现这个语言对于我的用途来说太复杂了,但是,我认为C语言中的一些C ++功能很酷......从那时起,经过多年的经验,我的观点完全改变了。 There's no way I will code in C anymore. 我再也不能用C编码了。

For me, there's no point: It's like limiting myself to move with a bike, when I could have the choice to use a bike, a car, a plane, or even my legs. 对我来说,没有意义:当我可以选择使用自行车,汽车,飞机甚至我的腿时,这就像限制自己骑自行车一样。

As long as your C++ compiler is Ok, and your developers know C++ (or want to learn C++), C++ can do everything C can (and usually better) and much more (remember, this is a personal viewpoint). 只要您的C ++编译器没问题,并且您的开发人员知道C ++(或者想学习C ++),C ++就可以完成C所能做的所有事情(通常更好)以及更多(请记住,这是个人观点)。

Post Scriptum: Is C++ used on critical apps? Post Scriptum:C ++是否用于关键应用程序?

Apparently, C++ is used in the F-35 (the "F-22 lite"). 显然,C ++用于F-35(“F-22 lite”)。 The following document is interesting because it shows what C++ features are really zero cost (and thus, have positive impact), and what C++ features could have a negative impact on the software: 以下文档很有意思,因为它显示了C ++的功能实际上是零成本(因此产生了积极的影响),以及C ++的哪些功能可能对软件产生负面影响:

http://www.stroustrup.com/JSF-AV-rules.pdf http://www.stroustrup.com/JSF-AV-rules.pdf

If C++ is good enough for this plane , then I guess C++ is good for a lot of less ambitious projects... 如果C ++足够好用于这个平面 ,那么我猜C ++对很多不那么雄心勃勃的项目都有好处......

:-) :-)

Pick the known evil 选择已知的邪恶
If you/your shop has a lot more more experience with C than with C++, stick with C. I wouldn't easily introduce a new technology on a mission critical project. 如果您/您的商店在使用C方面比使用C ++有更多的经验,请坚持使用C.我不会轻易地在关键任务项目中引入新技术。 Most if the time, qualification of developers is much more important to project succes than your toolset. 大多数时候,开发人员的资格对于项目成功而言比工具集更重要。

Pick the better toolset 选择更好的工具集
On many microcontrollers, compiler technology is five or ten years behind the consumer desktop platforms. 在许多微控制器上,编译器技术落后于消费者桌面平台五到十年。 A good compiler will make you stop worrying about micro-optimizations, with a decent profiler (that is ready and armed) you can skip a lot of guesswork and custom measurements. 一个好的编译器会让你不再担心微优化,有了一个不错的分析器(已经准备好并配备了武器),你可以跳过大量的猜测和自定义测量。

... it's how you use it ......这就是你如何使用它
All else being equal, it's not the choice of the language, but how you use it. 在其他条件相同的情况下,这不是语言的选择,而是你如何使用它。 The performance problems of C++ are pretty limited: C ++的性能问题非常有限:

  • More features available also means more features you could use incorrectly. 更多可用功能还意味着您可能使用的功能不正确。 There's a temptation to use them because they sound cool, and what could go wrong? 有使用它们的诱惑,因为它们听起来很酷,可能出现什么问题?
  • Some more things to know the relative/absolute cost for (eg member function calls, virtual calls, exceptions, ...) 还有一些事情要知道相对/绝对成本(例如成员函数调用,虚拟调用,异常......)
  • A piece of code can have more different meanings depending on other code, so some performance implications are not as quickly visible as they are in C. 根据其他代码,一段代码可能具有更多不同的含义,因此一些性能影响不像C中那样快速可见。

All of these points are about the developers, not the language itself. 所有这些都是关于开发人员的,而不是语言本身。

You mention the word readtime which I understand means that performance is critical for you. 你提到了readtime这个词,我理解这意味着性能对你很重要。 On this point, I can say that C++ does not give a performance hit when compared to C and C++ can and is used in Operating System Kernels (Symbian). 在这一点上,我可以说,与C和C ++相比,C ++不会产生性能损失,并且可以在操作系统内核(Symbian)中使用。

C++ does however offer idioms that can degrade performace. 但是,C ++会提供可降低性能的习惯用法。 Getters and Setters may cause extra code to be produced. Getter和Setter可能会导致生成额外的代码。 Vtable lookups will require a few extra instructions when compared to C code. 与C代码相比,Vtable查找需要一些额外的指令。

When it comes to maintainability, one also needs to bear in mind that system programmers will probably be more comfortable with C then they will be with C++. 在可维护性方面,还需要记住,系统程序员可能会更熟悉C,然后他们会使用C ++。

Language choice has very little to do with the three questions you asked. 语言选择与你提出的三个问题关系不大。 Choose the language that you are more experienced in, and that allows you to be more expressive. 选择您更有经验的语言,这样可以让您更具表现力。

Neither one has a specific advantage, really. 真的,两者都没有特定的优势。 It's more about how you use it than what, exactly, you're using. 它更多地是关于你如何使用它而不是你正在使用的。

specific answers: 具体答案:

  1. No, this generally depends on the developer's level of experience with the languages. 不,这通常取决于开发人员对语言的经验水平。 If he/she is more comfortable with C, then he will write C faster than C++(and vise-versa). 如果他/她对C更熟悉,那么他将比C ++更快地编写C(反之亦然)。
  2. Same as the above answer. 与上述答案相同。 Maintenance is just as much about the quality of code initially written and sometimes the quality of documentation. 维护与最初编写的代码质量有关,有时也与文档质量有关。
  3. The documentation syntax is exactly the same between the two languages. 这两种语言的文档语法完全相同。

由于更强大的数据类型和对象约束,你绝对应该使用C ++ :)

I think all three points you mentioned depend on the tool support. 我认为你提到的所有三点都取决于工具支持。 Since C++ is a lot harder to parse and also harder to analyze statically, it may be that some tools are only available for C. 由于C ++更难以解析并且更难以静态分析,因此可能有些工具仅适用于C.

  1. Is the development time of writing C++ code is longer then C? 编写C ++代码的开发时间是否比C长?

sometimes. 有时。 there are real-time applications which can't be implemented in C++ at all. 有些实时应用程序根本无法在C ++中实现。

  1. Is the maintenance cost of C++ code is cheaper then C? C ++代码的维护成本是否比C便宜? (I know that always will be changes in the code) (我知道代码总会有变化)

No. It's applications sensitive. 不,这是应用敏感的。

  1. Is it easy to documents code in C++ against C? 用C语言编写针对C的代码是否容易? (Documents that describe how the code works) (描述代码如何工作的文档)

In general they equivalent. 一般来说它们相当。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM