What and why are data structures in a program ?

  • Computer programs always process data (= information)

             Often:   a large amount of data !!!

  • Large amount of data (= information) must be organized so:

    • the program can find the needed information item quickly !

  • The structures used to organizes data in a computer program are called:

      • Data structures        

  • The 2 most commonly used data structures are:

      1. Arrays
      2. (Linked) lists        

  • CS171:

    • Arrays and linked lists have complementary strength and weaknesses (see: click here)

Primitive/simple variables and composite variables/data structure

  • Simple (or "unstructured") variables:

      • are variables of the primitive data types      

        E.g.: int x, float y, ...

  • The CPU has machine instructions that can process data of a primitive data type


  • Composite (or "structured") variables:

      • are variables that are composed of multiple primitive variables      

  • Examples of structured variables:

      • Array: a series of identically typed variables

      • Object: group of variables of different data types      

  • Composite variables are a.k.a. data structures

General storage characteristics of data structures

  • A data structure consists of multiple primitive variables (= components)

  • The components of a data structure are stored "close together" while obeying the alignment constraints

    Examples: how arrays and objects are stored in memory:

  • Note:

    • There are no gaps between array elements because:

      • Each array element if of the same data type

Accessing data structures in high level programming languages

  • High level programming statements are translated into assembler codes...

    • Assembler code only provides load (ldrsb, ldrsh, ldr) and store (strb, strh, str) assembler instructions to access data in memory !!!


  • Therefore:

    • High level programming languages always provide access expressions to access one primitive variable within the data structure

    Example: accessing an array element and a member in an object

Information need to access (primitive) variables in memory

  • To read the value stored in a variable in memory, you need to use a load instruction:

        ldrsb  rN, [rM]  or  ldrsh  rN, [rM]  or   ldr  rN, [rM]
    

  • To update the value stored in a variable in memory, you need to use a store instruction:

        strb  rN, [rM]   or  strh  rN, [rM]   or   str  rN, [rM]
    


  • Therefore:

    • The information you need to access (= read or write) a variable in memory

    are:

    1. The address of the variable (= the info in register rM)

    2. The data type of the variable (to select the correct load/store instruction)

How to accessing a simple variable inside data structures

  • A label is used to mark the start of a data structure (called the base address)

  • A simple variable inside a data structure is located a certain number of bytes after the start location:

  • The distance from the start of the data structure is called:  

      • offset                           

  • The address of simple variable inside a data structure is computed as follows:

       address of var in data stru = base address + offset of var