Lab: Ownership

Today’s lab will ask you to apply some of the concepts from our reading about ownership. You may find it helpful to refer back to the reading as you work through the lab.

A. Signaling Intentions with Declarations

It is a good practice to design our programs to make their behavior as clear as possible. In C, that includes conveying information about which pointer parameters are inputs versus outputs. That is, which pointers will be used to modify memory, and which will only be used to read values.

The exercises below will ask you to write declarations for functions with the specified behavior. You should not write the body of the function, just its signature. For example, the following is a declaration for the strlen function:

size_t strlen(const char* s);

This function tells us, through its declaration, that it will read the string s but will not modify it.

Exercises

  1. Write the declaration for a function called sum_values that takes in an array of integers (and the array length). The function adds up the values in the array and returns the sum.

  2. Write the declaration for a function called integer_divide that takes in four parameters. The first two parameters are integers, which the function will divide. The function returns 0 on success, and 1 if the division is invalid (e.g. a divide by zero error). The result of the division is a quotient and a remainder. These results are written to output parameters, which are passed as pointers.

  3. Write the declaration for a function called arrays_equal that takes three parameters: two arrays of characters and a length (both arrays are the same length). The function compares the arrays character-by-character and returns false if the arrays contain different characters, and true if the arrays are exactly equal.

B. Deciphering C Standard Functions

The exercises below will ask you to make some inferences about C standard functions based on their signatures and the documentation you find in their man pages. Answer the following questions about each function:

  1. Which pointer parameters, if any, could the function use to modify memory?
  2. Does the function take ownership of any pointers?
  3. Does the function transfer ownership of any pointers to the caller?

Exercises

  1. Answer the questions above for the function nanosleep.
  2. Answer the questions above for the function strdup.
  3. Answer the questions above for the function memset.
  4. Answer the questions above for the function memcmp.

C. The getline Function

The C standard library includes a very useful—but sometimes cryptic—function called getline. This function handles some of the tricky aspects of expanding arrays for us so we can read in user input of unknown length up to a newline character.

The following code snippet uses getline to read lines from the user one at a time, then prints them out:

int main() {
  char* line = NULL;
  size_t line_capacity = 0;
  while (getline(&line, &line_capacity, stdin) > 0) {
    printf("You typed %s\n", line);
  }
  return 0;
}

If you decide to run this code example you will need to type ctrl+D to get the program to exit its while loop.

Complete the following exercises, which reference this code snippet.

Exercises

  1. What is the first parameter to getline and what is it used for? Your explanation should include a discussion of which part of the code owns versus borrows any pointers where relevant.

  2. What is the second parameter to getline and what is it used for? Your explanation should include a discussion of which part of the code owns versus borrows any pointers where relevant.

  3. What is the third parameter to getline and what is it used for? Your explanation should include a discussion of which part of the code owns versus borrows any pointers where relevant.

  4. What value does getline return and what does it mean? Your explanation should include a discussion of which part of the code owns versus borrows any pointers where relevant.

  5. There is an important step missing from the code example above. Explain what is missing and how you would fix the example. You may reference your answers from the previous exercises if they help with your explanation.