Skip to main content
Engineering LibreTexts

10.1: Array definition and access

  • Page ID
    76142
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Because arrays can mean so many different things in HLL, this chapter will give a more precise definition of array that is sufficient to allow its implementation in ARM assembly.

    The definition of an array as used in this text is “A multi-valued variable where all the values are the same size, and are contiguous in memory”. To understand this definition each part of it will be detailed. First the concept of a multi-valued variable is that a single program variable contains more than one value. For example, the Java statement “int a[] = new int[10];” creates a Java variable “a” that contains 10 int vales.

    The phrase “all the values are the same size” is used, as opposed to “all the values are of the same type”. For primitives these two statements are effectively the same in a HLL. However for the Java statement “Object o[] = new Object(10)”, the type of the array is Object, but the values stored can be any type of Object. The statement “o[0] = new Integer()” is valid because the array stores object references (or addresses), which are the same for all types of objects. Thus the array stores objects of different types, but the sizes are consistent.

    The term contiguous means touching or connected throughout in an unbroken sequence. This means that the array must be a single allocation in memory, and the values cannot be spread out through memory.

    To see how this definition is used, consider the following code fragment:

        int arr = new int[10] 
        arr[3] = 5;
    

    In this code fragment a single block of 320 bytes are allocated starting at a base address (baddr) in memory (for example, baddr = 0x2350). The address of an element in the array is calculated by the formula:

        address = baddr + index * size
    

    In this formula, the base address would be 0x2350, the index is 3, and the size of an integer is 4 bytes. Thus the address of the arr[3] is 0x2350 + 3 * 4 = 0x235C,and M[0x2350] = 5.


    This page titled 10.1: Array definition and access is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?