News for hackers. Stuff that matters.
Re:Simple Linux kernel memory corruption bug can lead to com... by geofft on Wednesday October 20, 2021 @09:45AM (#28930592)
Yes, but this is by far the least weird type of optimization, and the one you'd least want to turn off. It's the sort taught in undergraduate compiler design classes.

Here's a piece of code that would go slower if you turned it off:

    for (i = 0; i < 1000; i++) {
        struct foobar *ptr = malloc(sizeof(struct foobar));
        ptr->this = a[i];
        ptr->that = b[i];
        if (ptr->this > 0) {
            do_stuff(ptr);
        }
        free(ptr);
    }
You only need to write to ptr->that if the condition succeeds. (And frankly you only need to do the allocation at all if a[i] > 0.)

Compilers that don't do this get rejected for compilers that do.

There are no comments on this comment yet.