CS 240 Lab 6

Learning Goals & Reflection

CS 240 Lab 6

Learning Goals

Core Goals

Students can:

  1. Explain pointers and addresses:
    1. Explain how a pointer references other data.
    2. Explain what a pointer actually consist of in terms of bits.
    3. Explain what the unary * operator does in C, using the word ‘dereference.’
    4. Explain what the unary * type modifier means in C.
    5. Explain what the unary & operator does in C.
    6. Explain which part of the HW Arch model CPU pointers would be stored in, and which part of the CPU the data they point to would be stored in.
  2. Use gdb to debug C code:
    1. Run a program via gdb, set a breakpoint, and step through code.
    2. Use gdb commands to inspect the values of variables and show the stack of active function calls.
  3. Use valgrind to check C code for memory errors:
    1. Run a program with valgrind and observe the resulting report.
    2. Identify which parts of your code to investigate based on valdgrid reports.
  4. Read and write C code using pointers:
    1. Identify which variables are pointers in C code, and the types of data they point to.
    2. Identify the types resulting from expressions involving the * and/or & operators, and whether or not they match the types of variables they are assigned to.
    3. Identify pointer dereferences which point to uninitialized data and therefore lead to undefined behavior.
    4. Declare array, string, and pointer variables, using static initial data.
    5. Use printf to print the value of a pointer as well as the value of the data it points to.
    6. Write a while or for loop that uses pointer arithmetic to iterate through an array or string.
  5. Explain how arrays and strings are implemented in C:
    1. Identify which element of an array or character of a string is being accessed by code that uses pointer arithmetic.
    2. Explain why an array in C must always be accompanied by extra information, and what that information is.
    3. Explain why strings do not need this extra information.

Stretch goals

  1. Explain pointers and addresses:
    1. Explain what happens when the & operator is applied to a variable which is storing a pointer.
  2. Read and write C code using pointers:
    1. Give an example of code that will cause a segmentation fault.
  3. Explain what malloc does at a high level:
    1. Explain what argument malloc takes and what result it returns.
    2. Explain what free is used for.

Extra goals

  1. Explain what malloc does at a high level:
    1. Explain why we always cast the result from malloc.

Reflection

Fill out the CS 240 Lab 6 Reflection form to complete today’s lab reflection.