News for hackers. Stuff that matters.
Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @07:51AM (#37819421)
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?
Re:My personal C coding style as of late 2023 by habibur on Monday October 09, 2023 @08:03AM (#37819536)
In 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.

Re:My personal C coding style as of late 2023 by kovacs_x on Monday October 09, 2023 @08:15AM (#37819631)
sure... 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?

Re:My personal C coding style as of late 2023 by habibur on Monday October 09, 2023 @11:49AM (#37821664)
you 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.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @08:48AM (#37819898)
> 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.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @12:57PM (#37822471)
> 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.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @01:25PM (#37822837)
You can also do that with C++.
Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @01:42PM (#37823076)
No, 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.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @02:05PM (#37823288)
> 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.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @02:13PM (#37823373)
> 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.

Re:My personal C coding style as of late 2023 by rtz121 on Monday October 09, 2023 @08:50AM (#37819920)
> - I prefer functions over classes.

I also prefer apples over brooms.

Re:My personal C coding style as of late 2023 by sebastianz on Monday October 09, 2023 @08:16AM (#37819640)
> it came with all the batteries included

Sometimes you do not want, or need, all the batteries.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @08:44AM (#37819867)
Smart pointers alone make the switch worth it. Why wouldn't you want that?
Re:My personal C coding style as of late 2023 by harpiee on Monday October 09, 2023 @09:59AM (#37820514)
Smart 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.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @11:26AM (#37821419)
What 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.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @01:07PM (#37822618)
Because 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.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @01:27PM (#37822877)
Sometimes 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?
Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @01:39PM (#37823027)
Everything around the rules of destruction in derived classes from a base class with/without a virtual destructor.

How about capture of values in a lambda within a loop? How to prevent a template expansion from killing you build process? When are move semantics sufficient for the std container classes and when are they not? What's the order of construction of multiple objects at file scope? When should a copy constructor be written so that the default shallow copy is prevented?

All of those are footguns, because if you do them wrong the program has runtime bugs without any compiler warnings.

They are all absent from C.

I'm typing on a phone, so won't go into detailw, but if you want to make such an insane claim, bear in mind that Scott Meyers himself said that C++ is too complex for him

You are not disagreeing with me when you make that claim, you're disagreeing with one of the world's foremost experts on C++.

Re:My personal C coding style as of late 2023 by uxp8u61q on Monday October 09, 2023 @02:10PM (#37823349)
> I'm typing on a phone

But you have an uncontrollable urge to write here? Someone's holding a gun to your head?

> You are not disagreeing with me when you make that claim, you're disagreeing with one of the world's foremost experts on C++.

I guess that settles everything then. Never mind that you're misquoting him.

Look, if you hadn't written the last two paragraphs, I'd have replied to your points, but they strongly indicate it would fall into deaf ears. You're clearly more interested in entertaining the peanut gallery more than actual discussion.

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @02:16PM (#37823409)
> I guess that settles everything then. Never mind that you're misquoting him.

I'm not misquoting him - you are free to provide a link to the context in which he said what he said.

The worlds foremost expert in C++, author of dozens of books on C++, disagrees with you. I'm merely agreeing with him.

> Look, if you hadn't written the last two paragraphs, I'd have replied to your points, but they strongly indicate it would fall into deaf ears. You're clearly more interested in entertaining the peanut gallery more than actual discussion.

The fact that you entered a thread about C practices, then got all salty when you tried to go with the "but why not use C++?" argument, then devolved into personal attacks is ... well "classy" is not the word I'd use.

EDIT: You can't respond to those points - those are all well-known footguns that are present in C++ but not in C. What were you going to respond with? "No, C++ doesn't have those!"?

Re:My personal C coding style as of late 2023 by lelanthran on Monday October 09, 2023 @12:53PM (#37822398)
Because 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.