4 ms·
If you use ++ in a sizeof expression...you're probably getting what you deserve. C may not be your friend, but it is fair.
by mpweiher 10y ago
If you use ++ in a sizeof expression...you're probably getting what you deserve. C may not be your friend, but it is fair.
- breadbox 10y agoThe issue is that sizeof is most often used in function-like macros, and the user may not even realize that it's a macro. (Or that sizeof is involved in it.)
- tobyhinloopen 10y agoWell the user should check what that thing is you're calling, or use it with caution (ie not using side-effects as params)
- marcosdumay 10y agoThe reasonable places you can use `++` and `--` in C are exclusively: - In a bare expression, with only the variable: `i++;` (That's what usually goes into `for`.) - In a simple assignment, without any extra expression: `int a = b++;` - While updating an array index that appears nowhere else: `v[i++] = 0;` Using it anywhere else is asking for trouble. In C++, using the last one is also asking for trouble.
- syncsynchalt 10y agoThe deeper you get into C++ the more you realize that Swift made the right call by removing all four inc/dec operators.
- marcosdumay 10y agoIn-place manipulation of values is a troublemaker. C is too old for its creators to know that, given that it's the language where people got to learn that. But no newer language should carry them.
- RubenSandwich 10y agoI'm not sure what a fair or unfair language would be. Is an unfair language one who's compiler output is non-deterministic? If that is the case then C fails that test with undefined behavior. What I'm really trying to say is that as an industry we need to stop proping up C as if it's some golden god. Was it a good compromise for it's time? Of course but we can and should do better because it's clear that writing 'good' C code might be beyond the realm of man.
- jackmott 10y agoIf we want to do that, we need to make it possible to fully harness the abilities of the computer in other languages. Just as a litmus test: In how many languages can you cause the SIMD shuffle instruction to be used, explicitly? How many have compilers that can take advantage of it? JVM / .NET languages don't do this (I know .NET can do SIMD, but it can't do shuffle). Rust can maybe do it? Not sure on the status. Dlang SIMD support seems only partial. Which can do a popcnt explicitly or automatically? There are lots of little holes in other languages just in the space of "Can I even use the instructions the cpu has". Then of course being able to control memory completely is a huge advantage in some domains. Any language that lets you do that will have most of the same danger problems as C or C++ I'm sure you could come up with something that is a little safer with as much power, but it isn't so easy for someone to just stop using C/C++ right now if they need that control.
- RubenSandwich 10y agoI would answer that you can use Rust to your first question. But I agree with your general sentiment that currently C/C++ is the only choice for certain domains. But there is nothing theoretically stopping us from creating safer system languages. (See Rust and Swift, much less so Swift. But Apple is working towards that goal with the option to disable reference counting in a future version which will give much more predictable performance.) My argument is that we need to stop pretending that C/C++ is not worth improving. Every time someone criticizes a gotcha of C/C++ HN gets very defensive. We need to kill our gods if we want better ones and I personally think we need better system programming languages because it is far too easy to write bad C/C++ code.
- Sir_Substance 10y ago>If you use ++ in a sizeof expression...you're probably getting what you deserve. For every developer who reads your comment and learns something, there's going to be one who commits a bug based on this issue to a production repo without realizing it. When it comes to discovering this kind of minutia in programming languages, it basically goes down one of two ways: Either you randomly stumble across information about the hazard on stack overflow (or hacker news, in my case), or you blunder right into it at least once. I call these kinds of quirks "landmines". I prefer my languages landmine-free, or as close to it as we could hope to achieve. This kind of behavior is definitely not something to make quips about, it's exactly the kind of language flaw that might cause government software to bill incorrectly calculated invoices to people who can't afford it, or overbook planes and cause confusion at airports. Maybe I've misinterpreted your comment, but your comment reads like you think of this sort of detail as "separating the men from the boys". It's totally not doing that. What it's really doing is randomly blowing up civilians.
- stcredzero 10y ago...you're probably getting what you deserve. I suspect we could use a crawler that detects this phrase applied to a programming language, and use that as a proxy metric for programming language design quality. (The more unique detections of the phrase, the worse the language.)