如何将object [] s数组存储在内存中? [英] How is an array of object[]s stored in memory?

查看:62
本文介绍了如何将object [] s数组存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,数组的元素连续存储在内存中,通过将所需索引和每个元素的大小与基本数组地址的乘积相加以找到地址,可以访问特定的数组的元素.

As I understand it, the elements of an array are stored contiguously in memory, and accessing a particular one it is done by adding the product of the desired index and the size of each element with the base array address to find the address of the element.

由于使用C#这样的语言,我可以创建一个 object [] s数组,并将所需的任何数据类型放入其中,该数组的每个元素如何存储(并保存)在一个数组中.如果我使用了不同大小的类型,但仍然允许随机访问,则长度是统一的吗?

Since in a language like C# I can create an array of object[]s and put whatever data type I want in it, how is each element of the array stored (and kept) at a uniform length if I used differently sized types while still allowing for random access?

推荐答案

这取决于所使用的语言以及对象"的含义-

This depends on the language in question, and what you mean by "object" -

正如您提到的C#,在C#(.NET)中, object [] 包含对单个对象实例的引用数组.数组是引用的数组-对象实例仍需要分配给数组的元素.引用具有统一的大小,但是对象实例本身是单独存储的,不必具有相同的大小.

As you mentioned C#, in C# (.NET), an object[] contains an array of references to individual object instances. The array is an array of references - the object instance still needs to be assigned to an element of the array. The references are of a uniform size, but the object instances themselves are stored separately, and do not have to be the same size.

在大多数语言中,当存储引用"或指针"而不是对象实例本身时,这是相同的.

This is the same in most languages when storing an array of "references" or "pointers", and not the object instances themselves.

这篇关于如何将object [] s数组存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆