Homework 5

  1. For the languages listed below, sketch the memory allocations needed for a rectangular array of 2 rows and three columns. Give the total amount of memory that is needed assuming that each element of the array has size S and an object reference has size W.
    1. A static array in the C language which uses a direct representation.
    2. An array in a Java program where multidimensional arrays are represented as arrays of arrays.
  2. Chapter 6, problem 10 - just do the access functions for column-major order.
  3. FORTRAN allows the programmer to create slices which take every nth element from a one dimensional array starting with a particular element (s). What would you use for the starting address (sometimes called the virtual origin) and element size in the descriptor for such a slice. Express your answer in terms of the starting address of the original array, the element size in the original array, the starting position s and the spacing between elements. Assume that the elements of both arrays are indexed from 0.
  4. Use the struct definition below to answer the following questions. Assume that char and bool variables take 1 byte, int and pointer variables take 4 bytes and double variables take 8 bytes.
        typedef struct lexeme {
            int token;
            int intValue;
            double doubleValue;
            char * stringValue;} Lexeme;
    
    1. How much memory does a record of this type take up?
    2. Give the offset in bytes from the starting address of the record for each element of the struct.
    3. Since the intValue, doubleValue and StringValue are never all used at once, you could use a union inside the struct. Draw a sketch to show how the data would be stored in this case. How many offsets are needed now?

This assignment can be turned in

Acceptable formats for electronically submitted assignments are text, OpenOffice or MicrosoftWord. I should be able to open your assignment with OpenOffice. Unless otherwise specified in the assignment, the entire assignment should be in a single document.