Skip to content
HomeVideosBooksBlogs
AllTop postsTopics
Podcasts
HistoryListsWatch laterLiked videosFollowing

The videos, books, blogs and podcasts Hacker News keeps recommending, all in one place.

VideosBooksBlogsPodcastsAboutPrivacySettings

Data from Hacker News, HackerTube, HackerBooks, HackerBlogs and Open Library. Credits · Not affiliated with Y Combinator.

HomeVideosBooksBlogsPodcasts
Ctrl K
    K

    Krister Walfridsson

    kristerw.blogspot.com · Personal blog · on HN since 2015

    Krister Walfridsson writes about compilers, mainly GCC and LLVM, covering optimizations, undefined behavior, and C and C++ details.

    Visit blogHN submissions
    Languages & CompilersSystems & Performance
    What HN says

    Commenters cite it in debates on undefined behavior and compiler optimization, and for his experiment building the 31-year-old GCC 1.27.

    Stats for nerds
    53HN people shared it
    61linked in comments
    1.5Kstory points
    Dec 2025last shared
    2016people per year2025

    Top posts on HN

    1. 1
      Useful GCC warning options not enabled by -Wall -Wextra
      5 people · 293 points · first shared 2017 · 166 comments on HN
    2. 2
      Why undefined behavior may call a never-called function
      14 people · 144 points · first shared 2017 · 169 comments on HN
    3. 3
      Code behaving differently in C90, C99, C11, C++98, and C++11
      2 people · 134 points · first shared 2016 · 17 comments on HN
    4. 4
      The structure of a GCC back end
      2 people · 105 points · first shared 2017 · Discussion on HN
    5. 5
      How undefined signed overflow enables optimizations in GCC
      8 people · 100 points · first shared 2016 · 90 comments on HN
    6. 6
      GCC code generation for C++ Weekly Ep 43 example
      1 person · 86 points · first shared 2017 · 17 comments on HN
    7. 7
      Interprocedural optimization in GCC
      1 person · 84 points · first shared 2017 · 8 comments on HN
    8. 8
      Building GCC 1.27 (first GCC with x86 support) (2019)
      4 people · 81 points · first shared 2019 · 66 comments on HN
    9. 9
      Type-based aliasing in C
      2 people · 73 points · first shared 2016 · 51 comments on HN
    10. 10
      Watching for software inefficiencies with Valgrind
      1 person · 67 points · first shared 2020 · 2 comments on HN

    What HN people wrote when they linked it

    • nmiloNov 2022

      I thought this was going to be an actually useful post about the details of UB, when it happens, and what it generally looks like, but instead it's just more of the same often repeated "if you write a program with UB your compiler will literally summon dragons and wipe your hard drive and kill you in your sleep."

      The truth is that practice diverges from theory, and, in practice, it is actually useful to talk about UB as a thing that happens and not as some mystical aura the compiler can sense or not. Leading to, in practice, almost all of these points being, if not flat-out wrong, at least unhelpful or misleading to the average programmer. For example:

      > 14. Okay, but if the line with UB is unreachable (dead) code, then it's as if the UB wasn't there.[1]

      The linked post ([1]) doesn't even showcase this phenomenon, it shows something completely different, where ignoring a 3 transmuted into a bool causes more UB. The problem was that you already had UB to begin with when transmuting a 3 into a bool. I want to see a godbolt output where the claimed:

      if (false)
      {
      run_ub();
      }

      actually causes problems.

      > 28. At least it won't completely wipe the drive.[2]

      This is just unhelpful for anyone trying to learn more about UB. UB can call parts of your code that would otherwise be dead, which is a useful piece of information to know. And if you literally write "rm -rf /" in these dead sections, then yeah, I guess UB is technically wiping your drive, but stating it…

    • zAy0LfpBZLC8mACApr 2017

      > First, it can assume the programmer is infallible and will never make a mistake, and use that to actually change the code as defined for performance reasons.

      You keep saying that it changed the code. It didn't. It compiled exactly what the code said. You dislike C, and that's OK, but that doesn't mean that a C compiler compiling C code into machine language that is semantically equivalent to the C code is somehow changing the program. It's not.

      > That is, it's trivial because the hard work of detecting the problem cases is already being done.

      No, it's not, you are still committing the same fallacy as before. The compiler doesn't "collect knowledge about undefined behaviour in the program", because that is useless knowledge for the compiler.

      The compiler collects knowledge that helps it reason about the program. A big part of that is tracking the range of values variables can take on. That is information that is useful in selecting how to express certain code in machine code. Undefined behaviour plays into this because operations that are defined to have undefined behaviour have no results that need to be considered as possible values of the variable that the result is stored in. So, if the compiler sees an if(x>0&&y>0){ x+=y;x/=2; }, with x and y being ints, it can derive that x will be positive, and therefore, for example, the division can be compiled to a right-shift.

      There is no code in the compiler that goes "well, there is this pointer dereference, so let's…

    • zrmAug 2019

      > Setting aside the question of what exactly "undefined behavior" means, why does a language spec have to include it? If there is behavior that cannot be defined, why not just omit it from the standard?

      The original reason was that there were things they didn't want to define. For example, signed integer overflow works differently on different hardware architectures. If they defined one behavior in the standard then compilers for architectures that didn't do it that way would have to do something inefficient to make it work the way the standard says it should rather than the way that hardware actually does it.

      Calling it "undefined behavior" lets the compiler do whatever the hardware does even if that means the program produces different results on different architectures. It also means that if some new architecture comes out that does it slightly differently, nobody can be surprised when compilers use the native overflow behavior for that architecture.

      The flaw was in giving compilers too much discretion. They were generally expected to implement one of the sane versions of signed integer overflow, and specifically the one corresponding to the relevant hardware architecture, but according to the spec they can literally do whatever they want. So we get this:

      https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html

      x + c < x -> false

      Which means you can't use that to check whether signed overflow occurred even when you know the…

    • ssokolowApr 2021

      > What does that even mean in the absence of a formal standard and competing compiler implementations? (Hint: nothing.)

      Undefined behaviour is stuff that you compiler's optimizers have been promised can never occur, so they are allowed to transform your code based on that assumption.

      Here's a post I made on Reddit in 2019 with a list of resources on what undefined behaviour is:

      https://www.reddit.com/r/rust/comments/dpxswt/rust_2020_lets_embrace_the_eternal_september/f5zwxuv/

      ...and here's the link to an example of what invoking undefined behaviour can do in C++:

      https://gcc.godbolt.org/z/0ubnbS

      ...and here's an explanation of why it does what it does:

      https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html

      (TL;DR: It injects a call to EraseAll because calling Do while it's still null is undefined behaviour, and Do is a static, so the optimizer determines that the only possible answer within the rules it was given is that code outside that compilation unit will have called NeverCalled to set Do = EraseAll before invoking main().)

    • rom-anticsFeb 2023

      That's a guarantee on the level of the hardware/OS, but hardware semantics are not the same as language/compiler semantics. Even if according to the source code you're dereferencing a pointer value 0x0 or 0x2, that doesn't mean the compiler-emitted machine code will end up telling the hardware to do the same.

      Remember this gem?

      https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html

      Once you trigger UB, all bets are off and your code could do anything. A segfault just means you spun the roulette wheel, bet it all on red, and got lucky your house wasn't bulldozed.

      Zig also uses LLVM under the hood, right? So it's subject to these same semantics. An LLVM pointer value cannot legally contain arbitrary non-null non-pointer integers such as 0x2. That's a dead giveaway of UB. And I doubt the emitted Zig code safety-checks every pointer dereference for a value less than 0x1000 before performing the dereference.

    • colandermanNov 2016

      While I like a lot of what's in here,

      for (size_t i = 9; i <= 9; --i)

      is a pretty terrible example to put in the second chapter. I would not let that line pass code review. There is no need or place for cutesy cleverness in C.

      EDIT: Ugh, just found this too:

      isset[!!largeA[i]] += 1;

      Not only is that confusingly cutesy, but largeA[i] is a double. Please DON'T write – or encourage beginners to write! – such smug code!

      EDIT2: In section 5 is the statement than unsigned integers "can be optimized best." This is flatly untrue on x86 and I suspect many other architectures. Compilers can and do take advantage of undefined signed overflow to optimize signed arithmetic; the same is not possible with unsigned arithmetic. See https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html

    Related

    John Regehrblog.regehr.org401 people

    Teaching C

    John Regehr
    Personal · last shared 2026
    LLVM Blogblog.llvm.org226 people

    Clang Is Now Used to Build Chrome for Windows

    LLVM Blog
    Company · last shared 2026
    Rust Blogblog.rust-lang.org533 people

    Announcing Rust 1.0

    Rust Blog
    Company · last shared 2026
    foonathanfoonathan.net35 people

    Carbon’s most exciting feature is its calling convention

    foonathan
    Personal · last shared 2026
    PVS-Studio Blogviva64.com135 people

    Criticizing the Rust Language, and Why C/C++ Will Never Die

    PVS-Studio Blog
    Company · last shared 2021
    Ralf Jungralfj.de99 people

    There is no memory safety without thread safety

    Ralf Jung
    Personal · last shared 2026
    Andrzej's C++ blogakrzemi1.wordpress.com35 people

    A bug in GCC that may cause memory leaks in valid C++ programs

    Andrzej's C++ blog
    Personal · last shared 2025
    ThePhDthephd.dev112 people

    Embed is in C23

    ThePhD
    Personal · last shared 2026