Everyone:

Next week, we will delve deeper into memory management and data structures in C. In particular, we will discuss how a typical address space is laid out, how to dynamically allocate memory using malloc (and how to release it with free), and how to create data structures with structs and unions.

TL;DR

The focus of this reading is to explore memory management and data structures in C and learn how to use valgrind and gdb.

Readings

The readings for this week are:

  1. Beej's Guide to C Programming

  2. Operating Systems: Three Easy Pieces

  3. Memory Layout of C Programs

Optional References

  1. Beej's Quick Guide to GDB

  2. Valgrind Quick Start Guide

  3. System Programming Wiki

  4. Julia Evans

Quiz

This week, the reading is split into two sections: the first part is a dredd quiz, while the second part involves one C program: str_rev.c.

To test the C program, you will need to download the Makefile and test scripts:

$ git switch master                   # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitHub

$ git checkout -b reading10           # Create reading10 branch and check it out

$ cd reading10                        # Go into reading10 folder

# Download Reading 10 Makefile
$ curl -LO https://www3.nd.edu/~pbui/teaching/cse.20289.sp24/static/txt/reading10/Makefile

# Download, build, and execute tests
$ make test

Questions

Given the following C program, allocations.c, which simply declares a series of variables:

/* allocations.c */

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    double x;
    double y;
} Point;

typedef union {
    char   c;
    int    i;
    float  f;
    double d;
} Block;

double GD = 3.14;

int main(int argc, char *argv[]) {
    int    a[]  = {5, 7, 4};
    char  *sp   = "Video Games";
    char   sa[] = "Runescape";
    Block  b    = {0};

    Point   p0  = {0, 0};
    Point  *p1  = NULL;
    Point  *p2  = malloc(sizeof(Point));
    Point  *p3  = malloc(5*sizeof(Point));
    Point **p4  = malloc(5*sizeof(Point *));

    return 0;
}

And the following C program, str_rev.c, which reverses the characters of each command-line argument:

/* str_rev: Reverse all characters in string */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *str_rev(const char *s) {
    char *t = malloc(strlen(s));
    strncpy(t, s, strlen(s));

    const char *r = s + strlen(s) - 1;
    char *w = t;
    while (r >= s) {
        *w++ = *r--;
    }
    return t;
}

int main(int argc, char *argv[]) {
    for (int i = 1; i <= argc; i++) {
        char *t = str_rev(argv[i]);
        puts(t);
    }
    return 0;
}

Record the answers to the following Reading 10 Quiz questions in your reading10 branch:

Programs

Given the provided str_rev.c and Makefile, you are to do the following:

  1. Modify Makefile to include a rule for the str_rev program. Be sure to use the CC and CFLAGS variables in your rule.

    Note: Consider the use of automatic variables.

  2. Modify str_rev.c so that it no longer has any uninitialized memory accesses, memory leaks, invalid memory acceses, or logical errors.

    Note: The str_rev must not modify the original string (hence the const char *). Consider the use of strdup.

Debugging

Use this as an opportunity to utilize valgrind and gdb to debug str_rev.c.

Once you have a working Makefile, you should be able to use the make command to run your recipes:

$ make clean                                  # Remove targets
rm -f str_rev

$ make                                        # Build targets
gcc -Wall -g -std=gnu99 -o str_rev str_rev.c

$ ./str_rev old school                        # Test manually
dlo
loohcs

$ make test                                   # Test scripts

Submission

To submit you work, follow the same process outlined in Reading 01:

#--------------------------------------------------
# BE SURE TO DO THE PREPARATION STEPS ABOVE
#--------------------------------------------------

$ cd reading10                        # Go into reading10 folder

$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/check.py                # Check reading10 quiz
Checking reading10 quiz ...
     Q1 1.50
     Q2 0.25
     Q3 0.25
  Score 2.00 / 2.00
 Status Success

$ git add answers.json                # Add answers.json to staging area
$ git commit -m "Reading 10: Quiz"    # Commit work

$ $EDITOR str_rev.c                   # Edit source code

$ make test                           # Build and Run tests
Checking reading10 str_rev ...
 str_rev (no arguments)                   ... Success
 str_rev (no arguments) (valgrind)        ... Success
 str_rev old school                       ... Success
 str_rev old school (valgrind)            ... Success
 str_rev 'old school'                     ... Success
 str_rev 'old school' (valgrind)          ... Success

   Score 2.00 / 2.00
  Status Success

$ git add Makefile                    # Add Makefile to staging area
$ git add str_rev.c                   # Add source code to staging area
$ git commit -m "Reading 10: Code"    # Commit work

$ git push -u origin reading10        # Push branch to GitHub

Pull Request

Remember to create a Pull Request and assign the appropriate TA from the Reading 10 TA List.

DO NOT MERGE your own Pull Request. The TAs use open Pull Requests to keep track of which assignments to grade. Closing them yourself will cause a delay in grading and confuse the TAs.