On Wed, 04 Feb 2026 at 10:49 +0000, Jonathan Wakely wrote:
On Wed, 04 Feb 2026 at 08:13 +0100, Søren Rune Nissen wrote:
Dear gcc

gcc 15.2 at -O2

Accumulating iota [1,10] in a vector

https://godbolt.org/z/Pz7GTqP1e

The optimizer somehow has a case where it doesn't see the allocation can be skipped, even though it successfully pre-calculates the sum without the allocation.

There's no allocation when

* the sum is returned from main
* the sum is returned from not_main
* the sum is printed from not_main

But if you write the code in main, and you print the sum instead of returning it, there's an allocation.

This is expected. As I wrote at
https://www.reddit.com/r/cpp_questions/comments/1qvbj44/comment/o3i0uae/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
GCC compiles main differently from not_main, and that's by design.

(and I just noticed you already replied there, so already read that!)

Reply via email to