How to calculate the offset of fields in a (list) struct/object

  • Suppose we have a struct definition as follows:

      struct myStruct
      {
          ...    ^
          ...    | offset
          ...    |
          ...    v
          int   x;
          ...
          ...
      }
    

    Then:

    • The offset of the field x (from the base address of the struct variable) is:

         number of bytes  used  for the variables  prior to x
      

How to calculate the offset of fields in a (list) struct/object

Consider the following struct Node (object) definition:

   struct Node
   {
      int           value;   // Data field
      struct Node * next;    // Link field  
   };  

 

How to calculate the offset of fields in a (list) struct/object

Consider the following struct Node (object) definition:

   struct Node
   {
      int           value;   // Data field   offset = 0
      struct Node * next;    // Link field   offset = 4
   };  

 

The offsets of the fields in each struct Node variable/object are as follows:

How to calculate the offset of fields in a (list) struct/object - Example 2

Consider the following struct Node (object) definition:

   struct Node
   {
      struct Node  *next;     // Link field  
      int           value1;   // Data field 1
      short         value2;   // Data field 2
      short         value3;   // Data field 3
   };  

How to calculate the offset of fields in a (list) struct/object - Example 2

Consider the following List (object) definition:

   struct Node
   {
      struct Node  *next;     // Link field    offset = 0
      int           value1;   // Data field 1  offset = 4
      short         value2;   // Data field 2  offset = 8
      short         value3;   // Data field 3  offset = 10
   };  
 

The offsets of the fields in each struct Node variable/object are as follows:

How to calculate the offset of fields in a (list) struct/object - Example 3

Given the following struct Node (object) definition:

   struct Node  
   {
      int           value1;   // Data field 1
      short         value2;   // Data field 2
      short         value3;   // Data field 3
      struct Node  *next;     // Link field  
   };  

 

The offset of the fields in Node variables defined by the above struct definition are:

   Offset(value1) = 0
   Offset(value2) = 4
   Offset(value3) = 6 (4+2)
   Offset(next)   = 8 (4+2+2)

 

We discuss how to obtain the base address of a Node object in the next slides.

Final comment: offset computation is actually more complicated

  • The complete process to compute the offset of a field is more complicated due to:

    • memory alignment requirments...

  • Alignment requirements:

    • An short type field must be placed at an address that is divisible by 2

    • An int type field must be placed at an address that is divisible by 4

    • Etc, etc

  • The memory alignment requirement will introduce gaps (= unused memory bytes) within the struct variable (or object)

    • The gaps will make the offset computation more complicated

  • Simplifying practice:

    • In CS255, we will only use struct definitions that has no gaps between fields.