6 ms·
> if it was smart it would have figured out that the value never was initialized. But that's false, which just goes to show that the compiler writers know way
by belovedeagle 9y ago
> if it was smart it would have figured out that the value never was initialized.
But that's false, which just goes to show that the compiler writers know way more about this than you do. There's nothing stopping this from being linked into a binary which doesn't even call main, or which calls NeverCalled, etc. And I bet you will also insist stamping your feet that of course programmers should be able to construct function pointers - to functions like, y'know, Never called - from arbitrary bit patterns. You know nothing, but you're convinced you know so much more than those stupid compiler writers.
- to3m 9y agoThe function called at program startup is named main, which this translation unit defines. No other may therefore define it. Binaries that don't run main are out of the scope of the standard, and so irrelevant to the discussion. Anyway, as a more general point: your argument is, basically, "the customer is wrong". But the customer is never wrong! Therefore your argument is invalid.
- belovedeagle 9y ago> The function called at program startup is named main, Incorrect. On Linux usually _start is called first and this can be freely changed at link time. LPT: Knowing a bit of JavaScript or whatever doesn't make you a systems engineer; don't act like it.
- to3m 9y agoRight, yes, sure, whatever. Since you've evidently got the experience that I apparently lack, you'll know that this point is irrelevant, since the topic at hand is Standard C, and not whatever some random implementation happens to do... so I'm not sure what your point is. But of course perhaps it would be obvious to a more experienced practitioner. C standard reference: https://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1 https://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1 (A freestanding environment may start anywhere - but such environments are unusual.)
- belovedeagle 9y agoNothing in the C standard prohibits parts of the executable from (a) being outside the "program" and thus many of the limitations of the standard, and (b) running before main. Therefore on Linux and Windows and almost certainly OS X too, main is not called first. Anyone who knows anything about systems programming knows this or should. (See also: the 90 million My First Disassembly blog posts out there.) But you know what else the c standard says? UB is UB. People in here complaining that UB is really unfair because it forces them to write correct programs are the ones bringing the real world into scope in this discussion. If you want to insist that we limit the discussion to the standard then you're just objectively wrong by definition, end of discussion. I was trying to be more productive than that.
- Avernar 9y ago>> if it was smart it would have figured out that the value never was initialized > But that's false Are you reading the same code the rest of us are? NeverCalled is never called. So Do is not explicitly initialized and therefore contains a null pointer because it's a static variable. Now compiler writers wanted their benchmark scores better so instead of crashing the program when Do is called, which happens in the unoptimized version, they decided to play fast and loose with UB. They just made code vanish. What I'm saying is that if the compiler can figure out that NeverCalled is actually called from somewhere then it's free to make these optimizations. But if it knows it's not called then it should either disable the optimization for that statement or better yet give a warning. > There's nothing stopping this from being linked into a binary which doesn't even call main, or which calls NeverCalled, etc. Which is why I called for Whole Program Optimization to solve that issue. Since it looks like you did not bother to find out what that is and how it would solve that issue I'll explain it here. In Whole Program Optimization the compile is pushed down to the link phase. This lets the compiler see the who program and apply optimizations globally instead of at a file by file bases. So it can tell if main is never called or if NeverCalled is called or not. > And I bet you will also insist stamping your feet Now you're attacking me instead of my arguments. Do you wish to have a civilized discussion or just resort to insults? Because if it's the latter I will just ignore you in the future. > which just goes to show that the compiler writers know way more about this than you do > You know nothing, but you're convinced you know so much more than those stupid compiler writers. I am a compiler writer so I do know what I'm talking about. It's a small personal project but it means I've been doing a lot of thinking and research about compilers. And eliminating UB is my current design focus. And if you reread what I wrote you can see I never called them stupid. They are quite smart and know what they are doing. But even a smart person can make bad decisions depending on their motivations. What I'm saying is that they are putting their skill towards exploiting UB instead of protecting programmers from it.
- belovedeagle 9y agoGo back to JavaScript, dear. You'll learn about real systems languages when you're older. Seriously. This just isn't how computers work. Main doesn't just get magically called in a vacuum. I don't see how a play project writing a compiler which is going to solve all those problems other compiler writers couldn't solve, while you don't even have a basic grasp of systems programming, helps your argument that you aren't being incredibly arrogant and naive in the face of more knowledgeable predecessors.
- dang 9y ago> You know nothing, but you're convinced you know so much more than those stupid For this and the other personal attacks you posted below, we've banned your account. It's unacceptable to conduct yourself like this on Hacker News.