Hacker News new | past | comments | ask | show | jobs | submit login

> C Compiler has to resort to UB to optimize many kinds "for" loops

The compiler is always permitted to assume the absence of UB. This is the case no matter what your C code is doing, and you're never further than one expression away from UB.

> It is so hard for programmer to unequivocally signal the intent like "we're working with this here chunk of memory malloc'd with this size and nothing ever beyond it and let me know if this is violated".

Indeed, and C pretty much stands alone here. In just about every other language, there's some automated means of keeping track of the size of an allocated block. Even C++ has this in std::array. The only exceptions I can think of are assembly, if that counts, and Forth.

> I don't understand why this state of affairs is not universally considered sordid but it's handwaved "just be aware of UB and pass the size along and it will be well"...

You're not alone in thinking this is a particularly reckless design decision in C. The great Walter Bright, designer of the D language and also a HackerNews regular, wrote a short article on this in 2009, called C's Biggest Mistake. [0] He even suggested a fix, of adding the option of fat pointers into C. I don't think the committee is going to adopt it though.

I recall reading somewhere a snarky take on this: A foundational principle of the C programming language is that the programmer is always right, even when they are wrong.

[0] https://www.drdobbs.com/architecture-and-design/cs-biggest-m... , see also discussion at https://news.ycombinator.com/item?id=1014533 , https://old.reddit.com/r/C_Programming/90uq7c/




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

Search: