3 ms·
Honest question: why C? I've been writing C++ firmware for SoCs, supposedly an area where C is supposed to be king, and C++ was just as good, except it came wit
by uxp8u61q 3y ago
Honest question: why C? I've been writing C++ firmware for SoCs, supposedly an area where C is supposed to be king, and C++ was just as good, except it came with all the batteries included. So, why C?
- habibur 3y agoIn my case : - I prefer functions over classes. - no mangling of exported names, the binary is re-usable as API. - in the long term, the C source code is more re-usable in other projects than C++ ones. and more like this.
- kovacs_x 3y agosure... and what about time spent debugging yet another unallocated/untimely freed/off by 1 pointer mistake? ps. you know you can write C++ code that is functional and use free functions primarily instead of putting everything in classes?
- habibur 3y agoyou can run valgrind and it will just point to you "you freed the memory on this line and then tried to reused it 10 lines bellow here, fix it." -- every time. And once you fix it, you have built a light weight library that you can use from any other language. Also these pointer manipulation is what gives C its power.
- uxp8u61q 3y ago> I prefer functions over classes. Then use functions? > no mangling of exported names, the binary is re-usable as API. Not once in my life have I seen someone use a binary as an API. > in the long term, the C source code is more re-usable in other projects than C++ ones. I don't even know what you mean by that.
- lelanthran 3y ago> I don't even know what you mean by that. He means that if you write your library in C it is callable from Python, Java, C++, Ruby, PHP, Python, Perl and more.
- uxp8u61q 3y agoYou can also do that with C++.
- lelanthran 3y agoNo, you cannot. Because C++ without exceptions is not C++, and those languages cannot catch exceptions, nor call overloaded functions, nor delete or create objects using new and delete, nor refer to fields with classes, nor call methods on objects.
- uxp8u61q 3y ago> Because C++ without exceptions is not C++ Write exception-free external APIs. It's not that hard. > call overloaded functions Write an API that doesn't overload functions? > nor delete or create objects using new and delete Write an API around that. You'd need to do it in C anyway. > nor refer to fields with classes What? > call methods on objects If you can call a function, you can call a method. Your complaint is basically "you cannot write C++ in Python". Duh.
- lelanthran 3y ago> Your complaint is basically "you cannot write C++ in Python". Duh. Maybe it is a complaint, but it's still a fact of life: your code is not reusable without wrapping it in C. The reality is that C++ is not as reusable as C is: ever wonder why there are so few reused libraries written in C++, while they are so many in C? Look on your system now - it's filled with C libraries, while the C++ libraries are probably a rounding error.
- rtz121 3y ago> - I prefer functions over classes. I also prefer apples over brooms.
- sebastianz 3y ago> it came with all the batteries included Sometimes you do not want, or need, all the batteries.
- uxp8u61q 3y agoSmart pointers alone make the switch worth it. Why wouldn't you want that?
- harpiee 3y agoSmart pointers for what? I don't use any dynamically allocated memory in my firmware projects. I do sometimes use statically allocated pools for things like packet buffers and allocate chunks out of them, but their lifetimes are not scope based, so automatic call of constructors/destructors would not be of any help.
- uxp8u61q 3y agoWhat do you want me to say? If nothing in your code owns a resource and needs to dispose of it when it's done, then obviously, you don't need smart pointers. I'm not here to evangelize, I'm trying to understand why someone would straight out refuse to use a language that offered more options if needed. I'm also wondering if you've heard about std::span, given your use case. I would be surprised if you weren't rebuilding much of that functionality.
- lelanthran 3y agoBecause it comes with everything else, notably it includes all the footguns or C and then adds orders of magnitudes more. Unless you're a solo developer, this is not a win. It's no accident that C++ is the only language in which its proponents have to self police their own teams to only use a subset of the language.
- uxp8u61q 3y agoSometimes I feel like I'm taking crazy pills when I go on HN. What footguns are there in C++ that aren't there in C?
- lelanthran 3y agoBecause the can read someone else C code without needing to look up almost all the quirks they used, while I cannot read someone's C++ code without having Google handy. Simplicity beats complexity almost every time.