The Most Surprising Optimization I've Seen Lately

It seems that under some conditions, gcc (such as gcc 10.1 targeting amd64 linux with -O optimization) compiles the following bad C++ code into non-leaking code. In fact, the generated code is equivalent to return 3; without any call to an allocating function:

class T {
    public:
    T(int i) : i(i) {}
    int i;
};

int should_leak_memory() { return (new T(3))->i;}


Entry first conceived on 7 December 2021, 15:07 UTC, last modified on 7 December 2021, 15:15 UTC
Website Copyright © 2004-2024 Jeff Epler