3 ms·
https://zevv.nl/nim-memory/ https://zevv.nl/nim-memory/ Local variables (also called automatic variables) are the default method by which Nim stores your var
by netbioserror 4y ago
https://zevv.nl/nim-memory/ https://zevv.nl/nim-memory/
Local variables (also called automatic variables) are the default method by which Nim stores your variables and data.
Nim will reserve space for your variable on the stack, and it will stay there as long as it is in scope. In practice, this means that the variable will exist as long as the function in which it is declared does not return. As soon as the function returns the stack unwinds and the variables are gone.
In Nim, all your data is stored on the stack, unless you explicitly request it to go on the heap.
Strings and seqs are allocated on the heap and accessed via a stack pointer, but the stack pointer controls their lifetimes, and semantically behaves like any other local variable.
- j-james 4y agoDo note that this was written before Nim's new runtime, ARC/ORC, became stable! The author has been working on a companion guide to the new runtime that goes over how it manages to be efficient with reference counting, but it's (very) much a work in progress.