Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

`s` may be null, and so the strlen may seg fault.


But that's undefined behavior, so the compiler is free to ignore that possibility.


> so the compiler is free to ignore that possibility

And that's what is wrong. This is the most unfriendly behavior towards the programmer.


Since the optimiser is allowed to assume you're not invoking UB, and strlen of null is UB, I don't believe that it would consider that case when optimising this function.


I understand that, but I don't agree that such optimizer behavior is worth it and I won't put it in my compilers.


I appreciate that greatly.


The notion that because it is undefined behavior means that the compiler is free to replace it with anything up to and including "launch nuclear missiles". This is just nuts.

If I program it to cause a null pointer seg fault, I expect a null pointer seg fault. If I program it to cause a twos complement overflow, I want a twos complement overflow.


Yeah, I feel the same way. It's refreshing to hear that that's not just because I'm insane. I think C compiler teams are sort of forced into this stupid shit because they don't have new CPU architectures to port to anymore, so, unless they want to go find new jobs, they're forced to waste their time and everyone else's by "improving" the compilers by increasing performance in riskier and riskier ways.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: