3 ms·
Why can't C programmers stop themselves from writing something like: size = (unsigned)(expression); ? The contradiction is stark. If a variable deserves the
by jeffbee 4y ago
Why can't C programmers stop themselves from writing something like:
size = (unsigned)(expression);
? The contradiction is stark. If a variable deserves the name `size` it deserves the type `size_t` as well.
- TheRealPomax 4y agoI think the real question is "if this is so bad, why does the language allow it". Because asking why people use the language in a way it was designed for has the obvious "because they can, and the language allows them to" as answer.
- duped 4y agoThere's nothing fundamentally bad about the code above. It's a smell but could make sense. Like for example if I'm writing an interpreter that runs on devices with limited memory size_t might be bigger than I need for the length of a buffer in user code, and using a shorter integer might be a useful optimization. This is really an example where you have a sharp tool and need training/oversight/experience to use it safely. And even with those things, mistakes happen. The same is true of power tools as memory unsafe systems code.
- anyfoo 4y agoMany things "could make sense", but often do not in the common case. Your optimization is a good example for something where jumping through explicit hoops would not only be palpable, but even help signify the intent of your code optimization, while protecting common code from nasty surprises. Otherwise, we could also go back to earlier C days and, for example, let the compiler be silent about assigning a pointer of the wrong type without an explicit cast. That makes sense a lot of times, but still I want the compiler to tell me that I passed a struct foo* where a struct bar* was needed, or worse, a struct foo**.
- KerrAvon 4y agoIt blew my mind (in a bad way) when i went from CodeWarrior to GCC, and suddenly basic pointer typechecking was a minor warning that you had to enable instead of a hard compile error that was on all the time. My takeaway was that Unix people were lax because they didn’t have to worry about corrupting system data structures like we did on classic MacOS, AmigaOS, Windows pre-NT, etc.
- duped 4y agoSure, but I think a lint to warn you that a variable named "size" should be size_t is complete overkill. What I'd rather see are better lints and a better culture around them instead of the cowboy attitude you see in a lot of systems code. Today, lints for C are pretty bad and the default attitude isn't to fix warnings but to disable them when compiling the software. Even worse is when distro maintainers patch the sketchy code instead of the 1p devs doing it right from the beginning. It doesn't matter what your warnings are if the attitude is to ignore them. And in this case, there are several warnings that would tell you the code smells and needs attention.
- TapamN 4y agoThat really seems like something that should generate warning, when a value assigned to a size_t was cast to a type smaller than size_t from a value that was as >= size_t. size_t v; //64-bit size_t v = (unsigned)0xff00000001; printf("%zu\n", v); //Prints 1 I don't get any warnings for this on GCC 9.4 or Clang 15.0.4 with Wall+Wextra. If you wanted to deliberately do that kind of cast, for some reason, you could prevent the warning like this: v = (size_t)(unsigned)0xff00000001;
- andix 4y agoEvery time I use C it feels like working on wiring without popping the breaker. A LOT of code just shouldn’t be C or C++.
- mlindner 4y agoAt F5 while I was there it was explicit coding practice to never use size_t, "because it wastes bytes". If you used size_t it'd get caught in code review and you'd be asked to remove it.
- acdha 4y agoThat's both scary and entirely believable with the number of CVEs they have due to 90s-style coding practices.
- tedunangst 4y agoWell, it's not a size, it's a page count.
- astrange 4y ago> If a variable deserves the name `size` it deserves the type `size_t` as well. That would be a mistake since size_t is poorly designed - it’s unsigned and you should use signed integer types whenever possible in C, as the Google C++ style guide says. This is more secure because UBSan is more likely to find bugs using it. Bit unfortunate the STL doesn’t like you doing it.
- pjmlp 4y agoBecause only the others are bad C programmers.