简体   繁体   English

如何在内存中为数组分配空间?

[英]How is space allocated in memory for an array?

I've been looking online for several hours now for a fairly simple answer to how space is allocated in memory for an array... 我已经在网上寻找了几个小时,以找到一个相当简单的答案,说明如何在内存中为数组分配空间...

Similarly, how are elements accessed in memory with indexing? 同样,如何通过索引在内存中访问元素?

In other words, how is the index used to locate a specific array element in memory? 换句话说,如何使用索引在内存中定位特定的数组元素?

Think of the array name as just an address in memory, and all of the values are stored sequentially from there. 可以将数组名称看作只是内存中的一个地址,所有值都从那里顺序存储。 If the array is multidimensional, each "element" in it is really just an address in memory pointing to the location of another array, so you have an array of arrays in that case. 如果数组是多维的,则其中的每个“元素”实际上只是内存中指向另一个数组位置的地址,因此在这种情况下,您将拥有一个数组数组。 Note: this link will probably help you visualize this! 注意:此链接可能会帮助您形象地看到它!

Arrays, like Objects, are allocated in the heap. 数组(如对象)在堆中分配。 Arrays are accessed much like they are in C or C++, with a base memory address and an offset. 数组的访问方式与C或C ++中的访问方式非常相似,带有基本内存地址和偏移量。

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

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