List (objects)
 

  • Goal:

    • Expose the storage and access of the (linked) list data structure

    • How are linked lists implemented in a high level language

  • Background information:

    • A list is an "object" (the term used in Java)

    • A list is an "struct(ure)" (the term used in C)

    We will therefore how C store structures first

How the C compiler process a struct definition
 

Example of a struct definition: (for simplicity: without gaps)

   struct myStruct
   {
      int x;  
      int y;  
      int z;  
   }  

 

Storage format of every struct myStruct variable:

How the C compiler process a struct definition
 

Example of a struct definition: (for simplicity: without gaps)

   struct myStruct
   {
      int x;  
      int y;  
      int z;  
   }  

 

Information extracted and stored by the C compiler:

  • The C compiler will record the following information:

       structure Name  | field name  |  field type  |  offset 
     ------------------+-------------+--------------+----------
         myStruct      |      x      |      int     |     0
         myStruct      |      y      |      int     |     4
         myStruct      |      z      |      int     |     8
    

    This table in the compiler is called: symbol table

Access expressions used to access the components inside a struct

C's syntax to access (= use) the components inside a struct:

  struct myStrunct a, b;    // Defines 2 struct varriables

  a.x  =  the  x  component inside struct variable a
  a.y  =  the  y  component inside struct variable a
  a.z  =  the  z  component inside struct variable a

  b.x  =  the  x  component inside struct variable b 
  b.y  =  the  y  component inside struct variable b
  b.z  =  the  z  component inside struct variable b 

Important fact:

  • All struct variables of the same type are stored in the same format:

Access expressions used to access the components inside a struct

C's syntax to access (= use) the components inside a struct:

  struct myStrunct a, b;    // Defines 2 struct varriables

  a.x  =  the  x  component inside struct variable a
  a.y  =  the  y  component inside struct variable a
  a.z  =  the  z  component inside struct variable a

  b.x  =  the  x  component inside struct variable b 
  b.y  =  the  y  component inside struct variable b
  b.z  =  the  z  component inside struct variable b 

Accessing the components inside a struct myStruct variable:

  Access  a.x:    base address of a + offset 0
  Access  a.y:    base address of a + offset 4
  Access  a.z:    base address of a + offset 8

  Access  b.x:    base address of b + offset 0
  Access  b.y:    base address of b + offset 4
  Access  b.z:    base address of b + offset 8