News for hackers. Stuff that matters.
Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @12:10PM (#23284083)
This! Casting the output of a function that returns (void*) is usually the first red flag of code not written by an experienced C programmer.
Re:The Ten Commandments for C Programmers (1987) by rrss on Saturday May 23, 2020 @12:18PM (#23284156)
it can also just indicate that when it was written it also had to compile as C++ for whatever reason.
Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @02:07PM (#23285027)
That's an even redder flat. Why would you even want or need that? I understand that sometimes it is convenient to write header files that can be read likewise by C and C++ compilers. But why would you ever need a piece of code to be compiled into an object by either C or C++ compiler, indistinctly?
Re:The Ten Commandments for C Programmers (1987) by flohofwoe on Saturday May 23, 2020 @02:32PM (#23285247)
That was essentially Microsoft's advice because they didn't want to keep their C compiler uptodate:

"We recommend that C developers use the C++ compiler to compile C code (using /TP if the file is named something.c). This is the best choice for using Visual C++ to compile C code."

From: https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...

Re:The Ten Commandments for C Programmers (1987) by enriquto on Saturday May 23, 2020 @02:39PM (#23285303)
I'm going to abstain from commenting on this topic. I am not emotionally prepared.
Re:The Ten Commandments for C Programmers (1987) by rrss on Saturday May 23, 2020 @03:32PM (#23285692)
There are several reasons why people would do this, but the decision is often not made by the person writing the code, so it may be a red flag to you, but it doesn't point to inexperience of the author.

In addition to the MSVC issue mentioned in the sibling comment, I've worked on projects that used clang thread safety analysis, which at the time (and maybe still today) was only available for C++, so the build for that analysis was configured to build all C source files as C++.

Re:The Ten Commandments for C Programmers (1987) by throwphoton on Saturday May 23, 2020 @12:15PM (#23284125)
What can you do with a void pointer without casting it, apart from comparing it to NULL?
Re:The Ten Commandments for C Programmers (1987) by eMSF on Saturday May 23, 2020 @12:21PM (#23284180)
You can (implicitly) convert it to another pointer type, e.g.

    int *buf = malloc(sizeof(*buf));
(It's noteworthy that while there's an implicit conversion above, there's no such thing as an implicit cast.)
Re:The Ten Commandments for C Programmers (1987) by unwind on Saturday May 23, 2020 @01:44PM (#23284861)
This is a pretty well-regarded answer, with motivation for why you should not cast [1].

Of course it's not something everyone agrees to, but at least a lot of people seem to.

[1] https://stackoverflow.com/questions/605845/do-i-cast-the-res...

Re:The Ten Commandments for C Programmers (1987) by throwphoton on Saturday May 23, 2020 @07:31PM (#23287595)
Interesting. Not casting a void pointer return value seems like a bit of a shibboleth for the "C is not a subset of C++" community.
Re:The Ten Commandments for C Programmers (1987) by enriquto on Sunday May 24, 2020 @10:26AM (#23291742)
A "better" shibboleth is using "new" and "old" as identifiers. It serves as a good barrier to avoid accidental C++ compilation.
Re:The Ten Commandments for C Programmers (1987) by glouwbug on Sunday May 24, 2020 @12:50AM (#23289191)
Generic data structures work great if they use a void* since malloc returns a void*. Just malloc your structs, and suddenly a single set of 10 or so functions can work with all data types to build queues, lists, hash maps, etc
Re:The Ten Commandments for C Programmers (1987) by _kst_ on Saturday May 23, 2020 @04:34PM (#23286161)
You can compare it for (in)equality to another void pointer.
Re:The Ten Commandments for C Programmers (1987) by microtherion on Saturday May 23, 2020 @01:35PM (#23284800)
You can assign it to any data pointer variable, and you can printf("%p") it, for example.