How stack handles variables in dynamically typed languages?

53 Views Asked by At

I'm trying to understand how stack is managed by dynamically typed languages.

Usually, in statically typed languages, local variables are stored in sequence on the stack (at least in terms of logical addresses). It is ok to understand: if I change the value of a variable, the space previuosly allocated to that variable is always the same and the new value will be put at the same space. In other words: the size of the space for the first value is the same for the second value of a variable and it will fit.

The issue is: in dynamically typed languages, a variable type can be changed, which means the size to hold a numeric value may be different of the size to hold a string. In this way (in Python):

    a = 300
    b = 400
    #do something
    a = "abcdefghijklmnopqrstuvwxyz"

If the stack behaves as statically typed languages, a is put on the stack with, lets say, 4 bytes of space and after that, b is put on the stack with 4 more bytes in the sequence of a.

What I can't understand is: where the string value will be allocated? It will not fit in the space of the previous value, because it will need more than 4 bytes.

I really searched for the answer and got a few responses: (i) the values after a will be shifted to let a's new value fit its place; (ii) the stack does not store values, just references them, and all identifiers are references to values in the heap (even for primitive types); (iii) each compiler will handle with different rules.

So, I tried to make an experiment with Python (CPython) because it has the id() function that returns its id which in case of CPython is the variable address. But, even for simple examples, it bugs me:

    def myfunction():
        a = 300
        b = 400
        print("Address of a = ", id(a))
        print("Address of b = ", id(b))
    
    
    myfunction()

And the results are:

    Address of a =  2168523566128
    Address of b =  2168523570416

Even when I don't change a variable type, the addresses don't seem in sequence. Or it has a gap of 4.288 addresses! 4kB between variables??? I know it doesn't really matter in terms of physical memory space because the addresses are not physical, but logical. But it does not behave as sequential allocation, which makes me feel the stack management gets complicated and slow to be handled.

If I change the type of a, as in:

    def myfunction():
        a = 300
        b = 400
        print("Address of a = ", id(a))
        print("Address of b = ", id(b))
        a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        print("Address of a = ", id(a))
        print("Address of b = ", id(b))

The output is:

    Address of a =  2932685843504
    Address of b =  2932685847792
    Address of a =  2932686637168
    Address of b =  2932685847792

And a gets far away from b after I assign a string of 300 bytes, but after variable a, and it seems the variable is reallocated.

In short: how stack of dynamically typed languages is handled considering the issues above?

0

There are 0 best solutions below