struct allocation
I am just curious.. Where the struct is allocated? On a stack (like in C++ without
new
) or on the heap?2 Replies
Within a function body, if you write
var x = Thing()
where Thing
is a struct, the fields of the struct are stored on the stack. However, structs are free to allocate additional memory on the heap to store some of their data. For example, the elements of a DynamicVector
are stored on the heap.
The simplest way to tell whether a data structure allocates heap memory is to look at its documentation or source code. (On that note, I'm looking forward to Mojo's standard library being open-sourced.)Thank you @Nick! 🙂