How are array elements stored when the array has null elements?

Nelssen :

I know this is basic but I'm not finding sources that confirms my interpretation.

The goal is to understand how arrays are stored in memory, below you will find my interpretation.

If a 4 size Array is like:

array[0] = 0

array[1] = null

array[2] = 2

array[3] = 3

Is it stored adjacently in memory without counting nulls, like this for instance?

[0], [2], [3]

if you add an element in a null position ([1]), than the next elements are pulled 1 position in memory?

[0], [1] (New), [2] (Pulled), [3] (Pulled)

References (Amoung others):

kaya3 :

A null is generally represented in memory by all the bits being zero. That is true regardless of whether the null is stored in an array or anywhere else.


I think there is some confusion in your question regarding what an array is. An array is a fixed-length sequence where every element in the sequence has the same type. The length of the array is determined when the array is created, and the length cannot change later (though you can create a new array with a different length if necessary).

The above is what you'll get from any textbook, but keep in mind that some languages like Javascript and PHP use the word "array" to mean things that are not arrays. A Javascript "array" is really a list, and a PHP "array" is really a strange hybrid between a list and an associative array. A Python list is not an array, though some sources mistakenly refer to it as one. These other data structures all allow you to insert an element at an index, but "insert an element" is not an operation on an array, because that would require increasing the length of the array by 1. The only operations on arrays are to get the value at an index, set the value at an index, and (if it's a length-prefixed array) get the array's length. You can use an array to represent a variable-length sequence, but you shouldn't confuse the array for what it represents.

There is no special treatment of null with regard to memory locations; if the array has a null at index 1, then the memory location corresponding to index 1 contains the memory representation of null. The whole point of an array is that each index corresponds with a memory location, so you can use the index to find the memory location directly. The formula for the memory address of an array element is:

(address for array[index]) = (address of array start) + index * (bytes per array element)

If the element at index 1 didn't take up a memory location, then the element at index 2 wouldn't be in the right place according to this formula.


The natural follow-up question is, if null in an array is represented in memory the same way as the number 0, how can the program tell whether the value is null or 0?

Since all the array elements have to be the same type of thing, null in an array only makes sense if the array elements are references, rather than primitive integers. For example, in Java you could have null in an Integer[] array but not an int[] array. So the zero bits representing a null would not be confused with the zero bits representing the number 0, because anyone reading an array of references is supposed to interpret the contents as references, not as integers; and anyone reading an array of primitive integers should interpret those bits as the number 0, not a null reference.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11288&siteId=1