M
Modular•9mo ago
gryznar

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
Nick!
Nick!•9mo ago
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.)
gryznar
gryznarOP•9mo ago
Thank you @Nick! 🙂
Want results from more Discord servers?
Add your server