简体   繁体   English

未初始化的变量内存分配

[英]Uninitialized variable memory allocation

JavaScript Example : JavaScript范例

Suppose I do this: 假设我这样做:

var i;

And never use i anywhere else in the program. 而且永远不要在程序的其他任何地方使用我。 Will it be allocated any memory? 会分配任何内存吗?

Or if I use, say i=2; 或者,如果我使用,请说i=2; after some lines.... will it be allocated memory at this point, or is the memory allocated during the creation of i? 在几行之后....它会在此时分配内存,还是在创建i期间分配内存?

C# example : C#示例

Suppose I do this: 假设我这样做:

dynamic i;

And never use i anywhere else in the program. 而且永远不要在程序的其他任何地方使用我。 Will it be allocated any memory (and if it will be, when? During compilation?)? 是否会为其分配任何内存(如果有的话,什么时候?在编译期间?)?

Or if I use, say i=2; 或者,如果我使用,请说i = 2; after some lines.... will it be allocated memory at this point, or is the memory allocated during the creation of i, or is it allocated during compilation? 在几行之后....它会在此时分配内存,还是在创建i期间分配内存,还是在编译期间分配内存?

Also, would there be any other differences regarding memory allocation in the two examples above except the differences that arise due to the fact that JavaScript is an interpreted language and C# is a compiled language? 另外,除了由于JavaScript是一种解释语言而C#是一种编译语言这一事实​​而引起的差异之外,上述两个示例中的内存分配是否还有其他差异?

In C#, the expression: 在C#中,表达式为:

var i;

can't be compiled in the first place; 不能首先编译; if we consider instead: 如果我们考虑改为:

int i; // or dynamic i;

then that can be compiled and may or may not be retained, but it depends on whether it is a field (object variable) versus a local (method variable). 那么可以对其进行编译,也可以不保留,但这取决于它是字段 (对象变量)还是局部 (方法变量)。 Fields are not removed; 字段不会被删除; however, the compiler is free to remove local variables as it sees fit. 但是,编译器可以根据需要自由删除局部变量。 Whether it chooses to do so can depend on a lot of things, but most notably: whether you are doing an optimized release build, versus a debug build. 是否选择这样做取决于很多事情,但最值得注意的是:您是在进行优化的发布版本还是调试版本。 Even if a local variable is clearly both written and read, the compiler can still remove it if it chooses - of course, the value will still exist on the stack, but not in a reserved location. 即使局部变量可以清楚地读写,编译器仍然可以选择删除它-当然,该仍将存在于堆栈中,但不在保留位置。

When the Javascript interpreter parses var i; 当Javascript解释器解析var i; and then executes the containing scope, it has to store the fact somewhere that the i variable is now defined in the current scope. 然后执行包含作用域,它必须将事实存储在当前作用域中现在已定义i变量的位置。 Futures references in this scope will access this particular variable in this scope. 此范围内的期货参考将访问此范围内的此特定变量。 Though implementation details are left to the implementor, the variable i is likely added to a particular scope object and thus has to consume some memory. 尽管实现细节留给实现者,但变量i可能会添加到特定的作用域对象中,因此必须消耗一些内存。

It is possible that if the variable is not referenced and it is in a contained scope without the use of things like eval() that the JS engine may be able to optimize it away. 如果未引用该变量,并且该变量处于包含范围内而不使用eval() ,则JS引擎可能会对其进行优化。 Whether or not it actually thinks it can do that and actually does so would have to be discovered by testing or studying of the source code. 它是否真正认为可以做到这一点,以及是否确实可以做到这一点,必须通过测试或研究源代码来发现。

Individual variables like this would likely consume only very small amounts of memory. 这样的单个变量可能只消耗很少的内存。 For this to be of major consequence, you would likely have to have thousands of these. 要使这个问题具有重大意义,您可能必须拥有数千个这样的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM