动态语言和变量分配
动态语言如何决定为变量分配多少内存? 例如。 编译器如何在不增加太多内存开销的情况下将variable= 5更改为variable ="xxx"? 什么时候使用硬件堆栈,什么时候使用内存堆?
How does a dynamic language decide how much memory to allocate for a variable?
eg. How does the compiler change variable= 5 to variable ="xxx" without too much memory overhead? When does it use the hardware stack and when does it use the memory heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器为每个变量分配足够的内存来保存指针以及语言运行时所需的任何元数据。 但我认为您的意思是询问为每个对象分配了多少内存。 在这种情况下,答案是它取决于对象的类型。 当变量被分配给不同的对象时,与该变量关联的指针会更改它指向的内容。
The compiler allocates enough memory for each variable to hold a pointer plus whatever metadata the language runtime requires. But I think you mean to be asking how much memory is allocated for each object. In that case the answer is that it depends on the type of object. When a variable gets assigned to a different object, the pointer associated with that variable changes what it points to.
当然,答案因语言而异——托管动态语言和较低级别的实现语言。 适用于 Perl 的内容不一定适用于 Python,适用于 Tcl 的内容也不一定适用于 Java 或 LISP,或者……好吧,它们算动态语言吗?
在 Perl 中,有一个名为 SV(标量变量)的 C 级结构,它包含不同版本的变量值的不同存储。 这些通常基于堆; 字符串的存储最终总是基于堆,尽管从未转换为字符串的纯数值可能位于严格位于堆栈上的 SV 中。 在 Perl 中,这些东西是引用计数的(并且是死亡的,或永生化的,以及各种其他有趣的术语)。 更复杂的类型(AV、HV、RV 等)基于 SV。
The answer, of course, varies by language - both the hosted dynamic language and the lower-level implementation language. That which applies to Perl does not necessarily apply to Python, nor does what applies in Tcl apply in Java or LISP or ... well, do they count as dynamic languages.
In Perl, there's a C-level structure that goes by the name SV (scalar variable) that contains different storage for different versions of the variable's value. These often heap-based; the storage for strings always ends up being heap based, though a pure numeric value that has never been converted to string might be in an SV that is strictly on the stack. In Perl, these things are reference counted (and mortalized, or immortalized, and all sorts of other interesting terms). More complicated types (AV, HV, RV, etc) are based on SV.