(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43352503

一篇 Hacker News 的讨论跟随着一篇关于 C 和 C++ 中未定义行为 (UB) 的文章。一些用户指出像 Rust 和 Go 这样的较新语言具有较少的 UB。一位用户建议为 C/C++ 添加一个“超级严格”模式来强制显式定义行为。其他人指出 Go 可能会通过数据竞争产生 UB,而 Rust 在“unsafe”模式下也有 UB。 讨论涉及 C++ 委员会不愿添加内存安全注解的情况,一位用户引用 `span::operator_at` 作为证据。一位用户认为 UB 是编译器为了适应多种实现而故意设置的“逃生门”,而不是意外。这与认为实现定义行为足以达到该目的的观点形成对比。带符号整数溢出被提及作为一个在较新的 C 标准中仍然存在的 UB 的例子。最后,一位经验丰富的 C 程序员批评链接的文章过时且写得不好。

相关文章
  • (评论) 2024-08-05
  • (评论) 2024-07-01
  • C 和 C++ 中未定义行为指南 (2010) 2025-03-17
  • (评论) 2025-03-14
  • (评论) 2024-07-06

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    A Guide to Undefined Behavior in C and C++ (2010) (regehr.org)
    43 points by GarethX 7 hours ago | hide | past | favorite | 15 comments










    This is an area the newer languages get right - I don’t think Rust or Go has any undefined behavior? I wish they would have some kind of super strict mode for C or C++ where compilation fails unless you somehow fix things at the call sites to tell the compiler the behavior you want explicitly.


    I think data races can cause undefined behavior in Go, which can cause memory safety to break down. See https://research.swtch.com/gorace for details.


    Safe Rust has no undefined behavior. Unsafe Rust does.


    > I wish they would have some kind of super strict mode for C or C++ where compilation fails unless you somehow fix things at the call sites to tell the compiler the behavior you want explicitly.

    The C++ language committee _does not_ want to add more annotations to increase memory safety.



    Not even annotations. The committee standardize this https://en.cppreference.com/w/cpp/container/span/operator_at

    So they clearly doesn't care so there's no point convincing them.



    Several programming languages can testify to the fact than a Benevolent Dictator For Life is not a panacea. Several more than testify that having a Committee to design the language is likewise not a panacea. Perhaps uniquely C++ can clarify for us that both is in fact worse than either alone.


    you realize UB is basically an escape hatch from the standard for compilers right? it's not like a flaw in the language, it's gaps negotiated by the standards committee (well for the most part i guess). so the reason new languages don't have UB is because new languages don't have multiple implementations (go definitely doesn't, does anyone use rust-gcc?).


    You only need implementation-defined behavior to grease the wheels of multiple implementations. You don't need the gaping void of undefined for that use.

    There's a big difference between "it'll be some number, not promising which one" and "the program loses definition and anything can break, often even retroactively".



    Potato potato. My point is UB isn't an accident it's intentional. Mind you I'm not saying it's great, just that it's not some kind of slipup.


    The first example (signed integer overflow) is no longer valid in newer standards of C. Now it should use the two-complement semantics and no UB.


    I believe they only standardized the two's-complement representation (so casts to unsigned have a more specific behavior, for example) but they did not make overflow defined.


    Yeah, signed integer overflow is as UB as ever. I've heard the primary reason for it is to avoid the possibility of wraparound on 'for (int i = 0; i < length; i++)' loops where the 'length' is bigger than an int. (Of course, the more straightforward option would be to use proper types like size_t for all your indices, but it's a classic tradition to use nothing but char and int, and people judge compilers based on existing code.)


    my opinion as a very experienced C system programmer:

    there must be better sources to guide people than a poorly written and infantilizing article from 15 years ago.



    Being a very experienced programmer, I'm sure you know many such sources. Can you share any?


    Perhaps you could draw on your wealth of experience to write one. I’d love to read it!






    Join us for AI Startup School this June 16-17 in San Francisco!


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



    Search:
    联系我们 contact @ memedata.com