are val variables stored in stack or heap? #736
-
I am writing an analysis of the koka programming language for a course I have in fundamentals of computer languages. I tried to analyse some compiled code to see how the language behaves, and i noticed that all variables - even ints - have dup and drop operations preformed on them. does that mean that all val variables are stored in the heap and nothing is stored on the stack? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Correct, Some values like small integers / strings are boxed in-place (no allocation). They will have a tag that distinguishes them from regular pointers. However, Koka has arbitrary precision integers, so if they get large enough, they won't fit in the pointer sized storage, and will be heap-allocated. Another barrier for stack allocation from a static analysis standpoint is that even values that look like they don't escape the stack, could be captured in a continuation with effect handlers. |
Beta Was this translation helpful? Give feedback.
Correct,
val
has nothing to do with storage - it is just an immutable binding. If you make something avalue
type though, it will be stack-allocated and passed via the stack itself (saying nothing about its fields - which will be stack / heap allocated based on their type). When you get to polymorphic functions though, there needs to be a uniform representation, so even stack-allocated values will temporarily be boxed for that.Some values like small integers / strings are boxed in-place (no allocation). They will have a tag that distinguishes them from regular pointers. However, Koka has arbitrary precision integers, so if they get large enough, they won't fit in the pointer sized storag…