7 ms·
This should probably carry a date stamp; my guess would be that it dates from 1990 or so. Most of the advice is still good, but I would object to (3): * Exces
by microtherion 6y ago
This should probably carry a date stamp; my guess would be that it dates from 1990 or so.
Most of the advice is still good, but I would object to (3):
* Excessive casting can be a problem in itself, as it hides genuine type errors (e.g. you think you're casting an int to long, but in reality the variable is a pointer)
* Casting NULL is pointless, as it's already defined by the standard to be a pointer constant, compatible with all data pointers.
* Much of the language about "prototypes" (a.k.a. standard C) was justified at the time, but no longer reflects reality nowadays. It would have to be a very exotic platform that still uses a pre-ANSI C compiler.
- enriquto 6y agoThis! Casting the output of a function that returns (void*) is usually the first red flag of code not written by an experienced C programmer.
- throwphoton 6y agoWhat can you do with a void pointer without casting it, apart from comparing it to NULL?
- eMSF 6y agoYou 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.)
- microtherion 6y agoYou can assign it to any data pointer variable, and you can printf("%p") it, for example.
- unwind 6y agoThis 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-result-of-malloc https://stackoverflow.com/questions/605845/do-i-cast-the-res...
- throwphoton 6y agoInteresting. Not casting a void pointer return value seems like a bit of a shibboleth for the "C is not a subset of C++" community.
- enriquto 6y agoA "better" shibboleth is using "new" and "old" as identifiers. It serves as a good barrier to avoid accidental C++ compilation.
- _kst_ 6y agoYou can compare it for (in)equality to another void pointer.
- glouwbug 6y agoGeneric 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
- rrss 6y agoit can also just indicate that when it was written it also had to compile as C++ for whatever reason.
- enriquto 6y agoThat'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?
- rrss 6y agoThere 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++.
- flohofwoe 6y agoThat 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-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...
- enriquto 6y agoI'm going to abstain from commenting on this topic. I am not emotionally prepared.
- deleted 6y ago[deleted]
- adrianN 6y agoI dislike implicit type conversions, but I think every cast needs to be accompanied by an assertion. Just sprinkling casts everywhere does nothing but silence important compiler warnings.
- phkahler 6y ago>> I dislike implicit type conversions... What do you think of "auto" in C++ ?
- bl0b 6y ago'auto' is only available to use when the type of a variable can be automatically deduced from its initializer, meaning the declaration would be redundant. It has nothing to do with type conversions. edit: now I see what you mean in the context of adding assertions to make type conversions safer, and how there could be some parallels with auto for that
- dllthomas 6y agoIt's an example of making something explicit implicit. Whatever the answer, it won't surface a contradiction; but that doesn't mean it's not an interesting, obliquely related question.
- phkahler 6y agoIt's implicit declaration, which seems worse in some ways than implicit conversions. After a conversion you know what type you have even if not the type you started with.
- codys 6y agoNot the parent, but `auto` in c++ improves the situation wrt implicit conversions as variables now automatically get the type of the thing assigned to them. IOW: less unintentional implicit conversions.
- CliffStoll 6y agoDated? #10 is from a 1980's MOTD message of the day: All the world's a VAX, And all the coders merely butchers; They have their exits and their entrails; And one int in his time plays many widths, His sizeof being N bytes. At first the infant, Mewling and puking in the Regent's arms. And then the whining schoolboy, with his Sun, And shining morning face, creeping like slug Unwillingly to school. -- A Very Annoyed PDP-11
- microtherion 6y agoHeh. Getting a response from you must be peak 1990 ;-) By the time I got started on Usenet "All the world's a VAX" was only in use as a proverbial misconception anymore. At the time, all the world was a Sun Workstation.
- neilv 6y agoYep, circa 1990, a SPARCstation or Sun-3 running SunOS 4 seemed the place to be, for having most software off the 'net build easily. I also worked on almost all the other engineering workstation platforms, for porting purposes, but a SPARCstation was my preferred main workstation, overall. (I was a kid who stumbled upon a dream job. So many expensive toys, so much I could learn, and this cool Internet thing...)
- icedchai 6y agoI bought a Sparc 5 back in 1996 or so, from a company going out of business. I felt like a king. Nobody had stuff like that at home back then.
- jacobush 6y agoWow... just seeing a post of Cliff Stoll is incredible. I love that TV movie and the book.
- kps 6y agoSubject: Ten Commandments For C Programmers Message-ID: <8771@utzoo.UUCP> Date: Wed, 14-Oct-87 17:35:26 EDT Subject: The Ten Commandments for C Programmers (Annotated Edition) Message-ID: <1990Nov4.021618.24681@zoo.toronto.edu> Date: 4 Nov 90 02:16:18 GMT
- neilv 6y ago1990, University of Toronto Zoology... one could make a make a good guess at the poster: https://en.wikipedia.org/wiki/Henry_Spencer https://en.wikipedia.org/wiki/Henry_Spencer :)
- earleybird 6y agohttps://www.lysator.liu.se/c/henry/index.html https://www.lysator.liu.se/c/henry/index.html
- ajross 6y ago> It would have to be a very exotic platform that still uses a pre-ANSI C compiler. Not "exotic" so much, really. Obviously those days are long behind us now, but for many years the very last refuge of K&R C was the bootstrap compiler for gcc. By the 90's, for revenue and product differentiation reasons all the big Unix vendors had moved their optimizing toolchains into their "developer" software packages and out of the base system. But for legacy compatibility reasons they still needed a working K&R C compiler in /bin/cc. So for almost two decades, the only contact working engineers had with pre-ANSI compilers was using them to bootstrap a real compiler (gcc) on their new, fancy, high performance, very mainstream and non-exotic workstations.
- eMSF 6y ago>* Casting NULL is pointless, as it's already defined by the standard to be a pointer constant, compatible with all data pointers. You should still cast NULL when passed as an unnamed argument to a variadic function. While NULL is defined to be a null pointer constant, that doesn't mean it has a pointer type (all integer literals with a value of zero are null pointer constants in C, but they have an integer type). (Strictly speaking, all unnamed pointer arguments to variadic functions should be converted to a compatible pointer type that the variadic function expects. For example, the printf-line of functions expect a pointer-to-void argument for a conversion specifier %p. If you pass a pointer-to-int instead, technically the behaviour is undefined.) (To whom it may concern: I would appreciate at least a rebuttal. It's not like this is a subjective matter.)
- monocasa 6y agoIt's unfortunate that you're getting downvoted; there's for sure real dragons at the intersection of the NULL literal and variadic functions since it's totally legal for NULL to be defined as such: #define NULL 0
- a1369209993 6y agoMuch, much more subtly nasty, it's also legal for NULL to be defined as: #define NULL ((void*)0) and then passed to a function that expects variadic int* parameters on a machine where register-sized addresses are word-aligned and thus int* has a different sizeof (and/or bit-level representation) than void*.
- microtherion 6y agoYou're right, I was misinterpreting the language of the standard. As a practical matter, C implementations typically define NULL as a void pointer, so the cast is not necessary in those implementations (In C++ pre C++11, NULL is always defined as a literal 0, so the cast is definitely necessary). But it appears the standard allows C implementations to define NULL as a literal 0 or as 0L as well, so the cast is needed in portable code.
- verall 6y ago
- WalterBright 6y ago> prototypes The following C code compiles today without complaint on gcc: foo(i, j) { return i + j; }
- burfog 6y agoAn interesting thing about the old system is that it could have checked data types just fine. This would have required linker support, but in the early years C didn't have the influence to make that happen. It would have been the tail wagging the dog. Linkers were designed for other languages, then used by C. With linker support, C++ would never have had to resort to name mangling. The foo(i,j) in your example could have caused the compiler to emit assembly directives to specify the argument types and return type. The assembler could have put these into the object file. The linker could have checked for compatibility when linking. The linker could have produced warnings, errors, or in some cases even thunks.
- microtherion 6y agoYes, I did not mean to imply that old-style code no longer compiles today, but that support for prototypes is near-universal nowadays.
- anaphor 6y agoI would add "Never cast malloc" to that as well. It's pointless and only required if you're writing C++, which is not C.
- yakshaving_jgt 6y agoFrom the following passage, I would have thought this would have been written as recently as today. > As a lamentable side issue, there has been some unrest from the fanatics of the Pronoun Gestapo over the use of the word ``man'' in this Commandment, for they believe that great efforts and loud shouting devoted to the ritual purification of the language will somehow redound to the benefit of the downtrodden (whose real and grievous woes tendeth to get lost amidst all that thunder and fury). When preaching the gospel to the narrow of mind and short of temper, the word ``creature'' may be substituted as a suitable pseudoBiblical term free of the taint of Political Incorrectness.