Importantly, the compiler is free to change this in either direction if it can prove that it can safely (unobservably) do so. For example, if it knows that a mutable structure doesn’t outlive the current function then it can stack allocate it. Or in the other direction, if an immutable structure is large and it would be better to heap allocate it than to pass it around on the stack then it can also do that.
Related topics
| Topic | Replies | Views | Activity | |
|---|---|---|---|---|
| Why Are Statically Sized Arrays Immutable? | 30 | 1341 | April 25, 2025 | |
| StaticArrays and allocations | 27 | 2538 | May 31, 2021 | |
| How to know when objects are stack or heap allocated | 7 | 1354 | November 8, 2024 | |
| How does SVector manage memory? | 7 | 665 | December 18, 2019 | |
| Fortran vs Julia stack allocated arrays | 18 | 6695 | November 29, 2018 |