首页 技术 正文
技术 2022年11月15日
0 收藏 380 点赞 2,646 浏览 3041 个字

http://stackoverflow.com/questions/1113819/arrays-heap-and-stack-and-value-types

Your array is allocated on the heap, and the ints are not boxed.

The source of your confusion is likely because people have said that reference types are allocated on the heap and value types are allocated on the stack. This is not an entirely accurate representation.

All local variables and parameters are allocated on the stack. This includes both value types and reference types. The difference between the two is only what is stored in the variable. Unsurprisingly, for a value type, the value of the type is stored directly in the variable, and for a reference type, the value of the type is stored on the heap, and a reference to this value is what is stored in the variable.

The same holds true for fields. When memory is allocated for an instance of an aggregate type (a class or a struct), it must include storage for each of its instance fields. For reference-type fields, this storage holds just a reference to the value, which would itself be allocated on the heap later. For value-type fields, this storage holds the actual value.

So, given the following types:

class RefType{
public int I;
public string S;
public long L;
}

struct ValType{
public int I;
public string S;
public long L;
}
The values of each of these types would require 16 bytes of memory (assuming a 32-bit word size). The field I in each case takes 4 bytes to store its value, the field S takes 4 bytes to store its reference, and the field L takes 8 bytes to store its value. So the memory for the value of both RefType and ValType looks like this:

0 ┌───────────────────┐
│ I │
4 ├───────────────────┤
│ S │
8 ├───────────────────┤
│ L │
│ │
16 └───────────────────┘
Now if you had three local variables in a function, of types RefType, ValType, and int[], like this:

RefType refType;
ValType valType;
int[] intArray;
then your stack might look like this:

0 ┌───────────────────┐
│ refType │
4 ├───────────────────┤
│ valType │
│ │
│ │
│ │
20 ├───────────────────┤
│ intArray │
24 └───────────────────┘
If you assigned values to these local variables, like so:

refType = new RefType();
refType.I = 100;
refType.S = "refType.S";
refType.L = 0x0123456789ABCDEF;

valType = new ValType();
valType.I = 200;
valType.S = "valType.S";
valType.L = 0x0011223344556677;

intArray = new int[4];
intArray[0] = 300;
intArray[1] = 301;
intArray[2] = 302;
intArray[3] = 303;
Then your stack might look something like this:

0 ┌───────────────────┐
│ 0x4A963B68 │ — heap address of refType
4 ├───────────────────┤
│ 200 │ — value of valType.I
│ 0x4A984C10 │ — heap address of valType.S
│ 0x44556677 │ — low 32-bits of valType.L
│ 0x00112233 │ — high 32-bits of valType.L
20 ├───────────────────┤
│ 0x4AA4C288 │ — heap address of intArray
24 └───────────────────┘
Memory at address 0x4A963B68 (value of refType) would be something like:

0 ┌───────────────────┐
│ 100 │ — value of refType.I
4 ├───────────────────┤
│ 0x4A984D88 │ — heap address of refType.S
8 ├───────────────────┤
│ 0x89ABCDEF │ — low 32-bits of refType.L
│ 0x01234567 │ — high 32-bits of refType.L
16 └───────────────────┘
Memory at address 0x4AA4C288 (value of intArray) would be something like:

0 ┌───────────────────┐
│ 4 │ — length of array
4 ├───────────────────┤
│ 300 │ — intArray[0]
8 ├───────────────────┤
│ 301 │ — intArray[1]
12 ├───────────────────┤
│ 302 │ — intArray[2]
16 ├───────────────────┤
│ 303 │ — intArray[3]
20 └───────────────────┘
Now if you passed intArray to another function, the value pushed onto the stack would be 0x4AA4C288, the address of the array, not a copy of the array.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896