#2282·benchmark

[BUG] GCC optimizes away the write with `benchmark::DoNotOptimize`

Author: SaiCharanMarrivadaCreated Aug 6, 2026Updated Sep 3, 2026

GCC16.1 optimizes away the write when we use benchmark::DoNotOptimize.

Minimal reproducible example

cpp
void f() {
    int *a = new int[1 << 16];
    for (int i = 0; i < 1 << 16; i++) {
        benchmark::DoNotOptimize(a[i] = 10); // this is optimized away by the compiler
    }
    delete[] a;
}

Compiler explorer(CE) link: https://godbolt.org/z/j538536ss. The CE link contains a possible fix for the issue.