Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are arrays stored contiguously in all languages?

I was reading a blog recently that made a big deal about array data being stored contiguously (specifically in Go), but it was my understanding that by definition, array data is stored contiguously. At least this is what all my algorithms and data structures study has led me to believe.

So my question is, is array data stored contiguously in all programming languages? And, specifically I want to talk about primitives like integers (or in the case of Java where I can have an array of Objects, are the references to the objects stored contiguously in memory? I know that the objects themselves can be anywhere).

like image 512
jcm Avatar asked Dec 19 '14 00:12

jcm


People also ask

Are arrays stored contiguously in memory?

An array stores its elements in contiguous memory locations. If You created the array locally it will be on stack. Where the elements are stored depends on the storage specification.

Are arrays stored contiguously in memory C++?

An array in C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array.

Are arrays stored contiguously in memory in Java?

Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements.

Do all languages have arrays?

Some languages don't have arrays or lists, but are expressive enough that linked lists can be easily defined.


1 Answers

It depends on what you define as an "array" on a language, although the traditional meaning of an array has to do with contiguous element placing.

For instance, in JavaScript, arrays are in fact hash tables, which means that data are not stored in contiguously but are hashed based on its indexing keys (using a hash function) to store the values in different addresses (questioned here), and this is only an example (in fact, I think that in dynamic languages it is a common phenomenon to have arrays as hash tables).

I suppose that this one answers your question with a no.

like image 77
Nick Louloudakis Avatar answered Nov 16 '22 22:11

Nick Louloudakis