Dynamic languages ​​and variable allocation

How does a dynamic language determine how much memory is allocated for a variable? eg. How does the compiler change variable = 5 to variable = "xxx" without unnecessary memory? When does it use the hardware stack and when does it use the heap of memory?

+1


a source to share


2 answers


The compiler allocates enough memory for each variable to hold the pointer plus any metadata required by the language. But I think you want to ask how much memory is allocated for each object . In this case, the answer is that it depends on the type of object. When a variable is assigned to another object, the pointer associated with that variable changes what it points to.



+4


a source


The answer, of course, depends on the language - both the hosted dynamic language and the lower-level implementation language. What is Perl is not necessarily Python, and what is used in Tcl is applied in Java or LISP or ... well, they are considered dynamic languages.



Perl has a C-level structure called an SV (scalar variable) name that contains different storage for different versions of a variable's value. They are often heap-based; storage for strings always ends with a heap-based radix, although a purely numeric value that has never been converted to a string might be in SV, which is strictly on the stack. In Perl, these things count (and mortal, or immortalized, and all sorts of other interesting terms). More complex types (AV, HV, RV, etc.) are based on SV.

+1


a source







All Articles