How is the memory allocation done for variables in scripting languages?

前端 未结 2 1645
有刺的猬
有刺的猬 2020-12-20 20:12

For example, in javascript

I can say

var x = 5;

Later I can do

x = \'a\';

and then



        
2条回答
  •  北海茫月
    2020-12-20 20:27

    Python uses a technique called reference counting, which basically puts a counter in the value. Each time a reference to a value is created, the counter is incremented. When a reference to the value is lost (for instance when you assign a new value to 'x'), the value is decremented. When the counter reaches zero, that means that no reference to the value exists, and it can be deallocated. This is a simplified explanation, but that's at least the basics.

提交回复
热议问题