8 ms·
Because it’s 2019! It really irritates me when people give arguments like this. I seriously don't care what year it is, I care that the code I write works. A l
by userbinator 8y ago
Because it’s 2019!
It really irritates me when people give arguments like this. I seriously don't care what year it is, I care that the code I write works. A lot of projects still use C89 because they want the widest portability. C99 isn't very new, but especially in the embedded and other niche industries, C89 (often with some extensions) is all you get.
That said, "= { 0 };" is almost always usable instead of a memset() and works in C89 too.
And finally, there is absolutely no reason to check if a pointer is NULL just before calling free() on it
This is advice I agree with. The nullcheck is in free() itself (IMHO a good design decision --- especially along error-handling and cleanup paths.)
- deathanatos 8y ago> but especially in the embedded and other niche industries, C89 (often with some extensions) is all you get. Embedded and niche industries are forcing their programmers to work with 30 year old tooling? (Instead of 20 year tooling?) I think at some point, writing yourself a transpiler from C99 to C89 becomes profitable… (The JS community did it for a lot less than that w/ babel…)
- Gibbon1 8y agoI remember reading a doc's for a library targeting ARM Cortex processors that crowed about it being C89 compatible. I pretty much spit on my keyboard. I also knew the code base was going to be kinda trashy and it was.
- userbinator 8y agoEmbedded and niche industries are forcing their programmers to work with 30 year old tooling? (Instead of 20 year tooling?) Why does how old it is mean anything at all? I personally choose C89 because of its wider compatibility, and I don't think I'm any worse off because of it. As the saying goes, "it's not about the tool, but how you use it." Incidentally, the best programmers I've worked with have also been the ones with the most conservative choice of tools. I think at some point, writing yourself a transpiler from C99 to C89 becomes profitable… (The JS community did it for a lot less than that w/ babel…) If anything, I think the JS community could learn a lot from the "C community" (a very loosely defined term...) --- the huge amount of churn with anything related to JS and web stuff in general turns me off.
- kllrnohj 8y agoDamn straight! That's why I exclusively use punch cards, for that good ol' reliability. Don't need no new fangled language crap! Sugar is for eating, not for syntax! If you're actually stuck with C89 you have my sympathy, like all the people stuck still maintaining FORTRAN and COBOL. But that's obviously a niche case with extreme limitations, it doesn't really need to be brought up every time someone mentions C99. Or C++11 for that matter.
- johannes1234321 8y agoCOBOL and Fortran are indeed still wide used, however I claim there is way more C89 than those two together. The point about C89 is that this covers a large area - all common Unix/Linux APIs and libraries are at least C89 compatible, if not written using C8 and as the GP mentioned embedded often depends heavily on such compilers. Thus usage goes far across industries with new libraries being created each day. COBOL and Fortan are way more in their fields (legacy business and science respectively) where more and more logic is build around those old systems using newer languages.
- kllrnohj 8y agoC89 compatible but not written in C89, which is the point. And almost certainly just C89 compatible because C99 didn't change the ABI, so that happened "for free". But the actual source, the thing people actually write in, is not C89. Linux heavily leverages new language and compiler extensions. It doesn't handicap itself with only using C89, because that's just silly.
- droithomme 8y ago#include <stdio.h> struct abc { int a; int b; int c; }; int main() { struct abc b = {2}; printf("a b c = %d %d %d\n ", b.a, b.b, b.c); return 0; } % c99 test.c % a.out a b c = 2 0 0
- klingonopera 8y agoAbsolutely! This is why I'll iterate through all and manually set each and every single element. You can even do it in one line, too: for (unsigned int i = 0; i < 3; i++) {b[i] = 2;} EDIT: Just noticed your code example uses structs. My example is for arrays, for which these observations are also correct.
- johnisgood 8y agohttps://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure https://stackoverflow.com/questions/10828294/c-and-c-partial... > The C and C++ standards guarantee that even if an integer array is located on automatic storage and if there are fewer initializers in a brace-enclosed list then the uninitialized elements must be initialized to 0. It does work for zero-initialization.
- klingonopera 8y agoI just had to google it: https://stackoverflow.com/questions/1912325/checking-for-null-before-calling-free https://stackoverflow.com/questions/1912325/checking-for-nul... There is history to this practice. And also, foresight: 1) I may want to redefine free(). 2) If I do, my free() does not null-check. 3) Which is why my main code would null-check. I'm an embedded developer, never do much on application work, and used to do that with C++, so I'm not too familiar with the heap allocation intricacies with C. But I'm quite sure that "delete" in C++ e.g. definitely needs to be null-checked (unless they changed that in some newer C++ revision, I always to try to code version-agnostic, and so far, have always managed). In practice, I find it better to manually null-check pointers before deleting their contents, it either gets optimized away anyways or is an extra redundancy in code-or platform-migration.
- userbinator 8y agoI may want to redefine free(). 2) If I do, my free() does not null-check. I think redefining standard functions to do something very unexpected is very poor decision, especially when it means you now have to burden all its callsites with an extra check. But I'm quite sure that "delete" in C++ e.g. definitely needs to be null-checked (unless they changed that in some newer C++ revision No, it was never needed in C++ either (which given its desire to be backwards-compatible with C, is not surprising.) From the C++98 standard section 5.3.5, paragraph 2: "if the value of the operand of delete is the null pointer the operation has no effect."
- klingonopera 8y agoYes, you're right about delete. I still do it, and will still do it, though. I have my reasons. No, I wouldn't redefine free(), I'd use my own terminology, but I'd search the code for it and replace it verbatim, and seeing that it apparently is standard practice for both C++ and C to null-check their pointers before deletion, I have all the more reason to do so, because my code assumed that it didn't. Delete shouldn't null check, it should delete memory at location, no matter what address. It should be up to the implementation to check that pointer is valid. EDIT: I should maybe add that the next line of code is in my case almost always "pointer = NULL" following "free(pointer)". The null-check is not just a technicality of checking of pointer-validity ("Pointer not null?"), but also code-syntactics ("Has this item not been deleted yet?").
- hedora 8y agoActually, I had some code that passed null to free(), and it segfaulted on some old version of Red Hat. (Not RHEL, Red Hat.)
- saagarjha 8y agoThat's either your platform's problem, in which case you should be very worried, or (more likely) you had some other undefined behavior somewhere else and it ended up biting you inside free.
- xondono 8y agoI was once forced to change the initialization of all the structs in an old codebase that used them for almost everything. The senior programmer that requested the change came from the application world and used the same argument. When I told him that the compiler gave lots of warnings about being compatible with C99 through an extension, he asked me to just disable the warnings and keep on going. We did some testing and pushed the update (we had remote devices connected via GSM) Two weeks later we were pushing a remote downgrade to the previous version because we started to detect anomalous bugs everywhere.