长度为1的数组是否与同一类型的单个变量大小相同? [重复]

时间:2022-01-16 22:17:24

This question already has an answer here:

这个问题在这里已有答案:

Fairly basic question, imagine

相当基本的问题,想象一下

int a = 5;
int[] b = new int[1];
b[0] = 5;

Do both a and b take up the same space in memory? I assume b is larger than a as it has to store the length of itself somewhere, so I thought it would be IntPtr.Size larger, but I am not sure.

a和b都占用内存中的相同空间吗?我假设b大于a,因为它必须存储自己的某个长度,所以我认为它会更大,但我不确定IntPtr.Size。

I am trying to write code where the length of the array is determined at runtime, and can be 1 or larger (<10). I didn't know if I should just make an array if the length is set to one, or to have a special case in the code and just use the underlying type for length == 1.

我正在尝试编写代码,其中数组的长度在运行时确定,并且可以是1或更大(<10)。我不知道如果长度设置为1,我是否应该创建一个数组,或者在代码中有一个特殊情况,只使用底层类型为length == 1。

I know that a is a value type while b is a reference type.

我知道a是值类型,而b是引用类型。

3 个解决方案

#1


2  

No, a and b will not occupy the same amount of memory.

不,a和b不会占用相同数量的内存。

The array container is an object in its own right. It will, somewhere, have to store data pertaining to the number of elements it contains. So it will have a non-zero size.

数组容器本身就是一个对象。在某处,它必须存储与其包含的元素数量有关的数据。所以它将具有非零大小。

#2


0  

Don't worry about it. Whatever you will gain with having an array of single element and a single value type is negligible.

别担心。无论您使用单个元素和单个值类型的数组获得什么都可以忽略不计。

For your question: As you already know, array is a reference type, so for 32 bit machines it will require a 4 byte reference and for 64 bit machine, it will require 8 bytes just to store reference. And then the extra memory required for each item in array.

对于您的问题:正如您所知,数组是一种引用类型,因此对于32位机器,它将需要4字节引用,对于64位机器,它将需要8个字节来存储引用。然后是数组中每个项目所需的额外内存。

so for

int a = 5; // 4 bytes

int[] array = new int[1]; //4 bytes or 8 bytes for reference (for 32 bit/64bit)
                          //4 bytes for `int` element.

#3


0  

a is a 32 bit int. It will take up 32 bits.

a是32位int。它将占用32位。

b is a reference type, it will take up one word of memory. On a 32 bit system, that it 32 bits. On a 64 bit system that's 64 bits.

b是一个引用类型,它将占用一个字的内存。在32位系统上,它是32位。在64位系统上,64位。

Of course the use of the array results in memory being consume outside of the space allocated for b itself, namely the actual array's memory that the b variable references, but when only comparing a and b as variables that's unrelated.

当然,使用数组会导致内存在为b本身分配的空间之外消耗,即b变量引用的实际数组的内存,但仅将a和b作为不相关的变量进行比较。

#1


2  

No, a and b will not occupy the same amount of memory.

不,a和b不会占用相同数量的内存。

The array container is an object in its own right. It will, somewhere, have to store data pertaining to the number of elements it contains. So it will have a non-zero size.

数组容器本身就是一个对象。在某处,它必须存储与其包含的元素数量有关的数据。所以它将具有非零大小。

#2


0  

Don't worry about it. Whatever you will gain with having an array of single element and a single value type is negligible.

别担心。无论您使用单个元素和单个值类型的数组获得什么都可以忽略不计。

For your question: As you already know, array is a reference type, so for 32 bit machines it will require a 4 byte reference and for 64 bit machine, it will require 8 bytes just to store reference. And then the extra memory required for each item in array.

对于您的问题:正如您所知,数组是一种引用类型,因此对于32位机器,它将需要4字节引用,对于64位机器,它将需要8个字节来存储引用。然后是数组中每个项目所需的额外内存。

so for

int a = 5; // 4 bytes

int[] array = new int[1]; //4 bytes or 8 bytes for reference (for 32 bit/64bit)
                          //4 bytes for `int` element.

#3


0  

a is a 32 bit int. It will take up 32 bits.

a是32位int。它将占用32位。

b is a reference type, it will take up one word of memory. On a 32 bit system, that it 32 bits. On a 64 bit system that's 64 bits.

b是一个引用类型,它将占用一个字的内存。在32位系统上,它是32位。在64位系统上,64位。

Of course the use of the array results in memory being consume outside of the space allocated for b itself, namely the actual array's memory that the b variable references, but when only comparing a and b as variables that's unrelated.

当然,使用数组会导致内存在为b本身分配的空间之外消耗,即b变量引用的实际数组的内存,但仅将a和b作为不相关的变量进行比较。