4 ms·
I haven’t worked with ffmpeg’s code, but I have worked with QEMU. QEMU has a lot of OOP (implemented in C obviously) that is supported by macros and GCC extensi
by variadix 2y ago
I haven’t worked with ffmpeg’s code, but I have worked with QEMU. QEMU has a lot of OOP (implemented in C obviously) that is supported by macros and GCC extensions. I definitely think it would have been better (and the code would be easier to work with) to use C++ rather than roll your own object model in C, but QEMU is quite old so it’s somewhat understandable. I say that as someone who mostly writes C and generally doesn’t like using C++.
- shmerl 2y agoWhat's the reason for ffmpeg to use C, also historic?
- defrost 2y agoFabrice also wrote the Tiny C compiler, so very much his language of choice .. For those used to the language it was seen as "lighter" and easier to add OO like abstractions to your C usage than bog down in the weight and inconsistencies of (early) C++ https://bellard.org/ https://bellard.org/ https://en.wikipedia.org/wiki/Fabrice_Bellard https://en.wikipedia.org/wiki/Fabrice_Bellard
- lelanthran 2y ago> weight and inconsistencies of (early) C++ Since very little is ever removed from C++, all the inconsistencies in C++ are still there.
- maccard 2y agoEvery language has inconsistencies, and C is not stranger to that. Much of c++’s baggage is due to C and you carry the same weight. That’s not to say that initialization isn’t broken in C++, but just like many features in many languages (off the top of my head in C - strcpy, sprintf, ctime are like hand grenades with the pin pre pulled for you) don’t use them. There’s a subset of C++17 that to me solves so many issues with C and C++ that it just makes sense to use. An example from a codebase I spend a lot of time in is int val; bool valueSet = getFoo(&val); if (valueSet) {} printf(“%d”, val); // oops The bug can be avoided entirely with C++ if (int val; getFoo(&val)) // if you control getFoo this could be a reference which makes the null check in getFoo a compile time check {} printf(“%d”, val); // this doesn’t compile.
- ablob 2y agoYou could write this in C, no? { int val; if (getFoo(&val)) { ... }} Both ways of expressing this are weird, but stating that this can't be achieved with C is dishonest in my opinion.
- i_am_a_peasant 2y agoit's not that weird to explicitly limit the scope of certain variables that logically belong together. But i agree the C++ if(init;cond) thing was new to me.
- maccard 2y agoLike everything in C++, it has it's share of footguns if (Foo* f = GetPtr(); f->HasValue()) {} // wrong; f can be null vs if (Foo* f = GetPtr(); f && f->HasValue()){} Is probably the biggest pitfall. Especially if you're used to this: if (Foo* f = GetPtr()) { f->DoTheThing(); // this is perfectly safe. }
- maccard 2y agoIf I reformat this, { int val; if (getFoo(&val)) { } printf("%d", val); } The bug is still possible, as you've introduced an extra scope that doesn't exist in the C++ version. Also, this was one example. There are plenty of other examples.
- ho_schi 2y agoFor C users. And C++ users: In C++ we can declare variable in the while or if statement: https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/if https://en.cppreference.com/w/cpp/language/if It's value is the value of the decision. This is not possible with C [1]. Since C++17 the if condition can contain an initializer: Ctrl+F if statements with initializer https://en.cppreference.com/w/cpp/language/if https://en.cppreference.com/w/cpp/language/if Which sounds like the same? Now you can declare a variable and it value is not directly evaluated, you also can compare it in a condition. I think both are neat features of C++, without adding complexity. [1] Also not possible with Java.
- emmelaich 2y agoMuch easier to link / load into other language binaries surely.
- adastra22 2y agoextern “C” works just fine.
- josefx 2y agoOnly if your entire API doesn't contain any C++.
- pjmlp 2y agoC is also C++, at least the C89 subset.
- procaryote 2y agoit actually isn't https://www.stroustrup.com/bs_faq.html#C-is-subset https://www.stroustrup.com/bs_faq.html#C-is-subset
- pjmlp 2y agoDo you usually post links without reading them? "In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C. Every C program can be written in essentially the same way in C++ with the same run-time and space efficiency. It is not uncommon to be able to convert tens of thousands of lines of ANSI C to C-style C++ in a few hours. Thus, C++ is as much a superset of ANSI C as ANSI C is a superset of K&R C and much as ISO C++ is a superset of C++ as it existed in 1985. Well written C tends to be legal C++ also. For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program. "
- layer8 2y agoC has less moving parts — it’s more difficult to define a subset of C++ that actually works across all platforms featuring a C++ compiler, not to mention of all the binary-incompatible versions of the C++ standard library that tend to exist — and C is supported on a wider variety of platforms. If you want to maximize portability, C is the way to go, and you run into much fewer problems.