Is C better than C++?

In the TAAE documentation and also on the audiobus forum, the point is stressed that "plain old C" is the best language to use for tasks on the audio thread. That c and c++ are more efficient than objective c and swift is clear to me. But what is the advantage of using plain C as opposed to c++?


  • In my opinion, and I'm not a language freak is that C is the closest you have to assembly. C++ is an extension of C and therefore it provides you stuff like class inheritance. If you encapsulate your everything in classes you will have an added cost of this underlying management. When you are writing audio stuff like a synth engine, with C++ you can encapsulate everything in objects but I think that will lead to slight less performance than if you were using plain C. Of course, implementing in C, you don't have the object classes flexibility and that will lead you to a more complex code structure. As always, each case is a case and profiling is the best tool to make decisions. This is only valid if you need to squeeze the performance the most. I'm building a synth engine and right now I prefer to have a flexible code structure provided by C++. I think that when I get to real performance improvements I will need to write critical processing sections in assembly, but that's another battle.

  • C++ is fine - generally, it's much of a muchness, really. Performance isn't a major concern on this fast hardware, and C++'s method calling system is pretty much the same as C's (plus a table lookup). There's none of the memory allocation/lock holding business that's in Objective-C which makes Obj-C so poor for audio.

    For building whole code modules, if it's a choice between C or C++, I'd probably advise C++, because it allows for better structuring.

    Obj-C has a lovely little property which is that you can mix it with pure C easily, treating instances just like they were a struct, accessing instance variables via the usual struct dereference operator. That's why I use plain C for audio, written as part of an Obj-C class. You get the convenience of Obj-C's lovely structure and object management, and the performance of C. But that's just me.

  • edited December 2015

    @Michael , could you give an example of how you refer C code from Obj-C? using extern "C" ? Thanks

Sign In or Register to comment.