JMU CS 430 Spring 2022

Programming Languages

Module 07 Lab

Work through this lab as a group, writing your answers individually on the provided work sheet. Note that the work sheet doesn’t have space for all of the questions; for the ones not included, just discuss the answer with your table before moving on. When you are done, scan and submit ONLY that work sheet in Canvas.

Part 1: Pointer Types

Consider the following C program:

int main() {
    int a = 123;
    int *b = &a;
    int **c = &b;
    // location A
    *b = 321;
    printf("%d %d %d\n", a, *b, **c);
    return 0;
}

1) Draw a picture of stack memory at location A. Label memory locations with their C variable names, and draw pointers as arrows pointing to their targets. (You do not need to turn in this diagram – you will check it yourself a bit later.)

2) What is the name of the “*” operator in C (e.g., on the two lines after location A), and what does it do?

3) How does your diagram change after the line following location A? What is the program’s output? (If the output cannot be determined, write “N/A”.)

4) Load PythonTutor.com/visualize.html and enter the above code (or use this shortcut). Run the code (make sure you’re in C mode) and confirm your answers to #1 and #3 above.

Consider the following C program:

int main() {
    char msg[] = "Hello!";
    msg[1] = 'o';
    char *p = msg;
    // location B
    *p = 'P';
    p++;
    *(p+4) = '?';
    printf("%s\n", msg);
    return 0;
}

5) Examine memory at location B using PythonTutor, and observe how the contents of memory change after each of the three lines following location B. (shortcut)

6) What is the program’s output? (If the output cannot be determined, write “N/A”.)

Part 2: Heap Pointers

Consider the following C program:

int main() {
    int *a = (int*)malloc(5*sizeof(int));
    int b = 2;
    int *p = &a[1];
    // location C
    a[0] = 777;
    a[b] = 45;
    *p = 9;
    p[2] = 12;
    p += b;
    *(p+1) = *a;
    // location D
    free(a);
    printf("%d\n", *p);
    return 0;
}

7) Examine memory at location C using PythonTutor, and observe how the contents of memory change between location C and location D. (shortcut)

8) What is the program output? (If the output cannot be determined, write “N/A”.)

9) Does the program leak memory? Are there any dangling pointers?

10) Describe briefly (in 1-2 sentences) how mark-and-sweep garbage collection would help deal with a problem identified in #9.

11) Describe briefly (in 1-2 sentences) how reference counters would help deal with a problem identified in #9.

12) What is a reference type and how is it different from a pointer type?

13) Does C have reference types, pointer types, or both? What about C++, Java, and Ruby?

Part 3: Struct and Union Types

Consider the following C program:

struct {
  int a;
  char b;
  double c;
} v;

int main() {
    v.a = 7;
    v.b = 'x';
    v.c = 1.23;
    // location E
    printf("%d %c %f\n", v.a, v.b, v.c);
    return 0;
}

14) Examine memory at location E using PythonTutor. (shortcut)

15) What is the minimum number of bytes necessary to store the struct variable (“v”) in the above program? (Assume that characters require 1 byte, integers require 4 bytes, and double-precision floating point numbers require 8 bytes.)

16) Suppose that on a particular system there are X different possible integers, Y different possible characters, and Z different possible double-precision numbers. As a function of X, Y, and Z, what is the total number of different possible values for the struct variable (“v”)?

17) Change “struct” to “union” in the first line. Consider how this would change what is stored in memory and what effect it would have on the output. Check your predictions using PythonTutor. What is the minimum number of bytes required to store the union variable?

18) As a function of X, Y, and Z (as defined in #15), what is the total number of different possible values for the union variable (“v”)?

Part 4: Type Equivalence

Suppose the following declarations have been made in a C-like language. For each context, circle all of the assignments that are valid, and cross out all that are not valid.

typedef float inches;
typedef float feet;
typedef struct { inches x; feet y; } box;
typedef struct { inches x; feet y; } bin;

inches a, b;
feet c;
float d;
struct { inches x; feet y; } m, n;
box o;
bin p;

19) Assume assignments require name equivalence unless at least one type is anonymous, in which case they only require structure equivalence.

  a = b      a = c      a = d        a = m

  m = n      m = o      p = o        m.x = d

  m.x = a    o.x = a    m.x = m.y    m.x = o.x

20) Assume assignments only require structure equivalence, regardless of type aliases.

  a = b      a = c      a = d        a = m

  m = n      m = o      p = o        m.x = d

  m.x = a    o.x = a    m.x = m.y    m.x = o.x

21) Assume assignments require name equivalence for primitive types and their aliases but permit structure equivalence for non-primitive types.

  a = b      a = c      a = d        a = m

  m = n      m = o      p = o        m.x = d

  m.x = a    o.x = a    m.x = m.y    m.x = o.x