Everyone:
Next week, we will delve deeper into memory management, data structures, and binary numbers 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.
The focus of this reading is to explore memory management, data structures, and binary numbers in C and learn how to use valgrind and gdb.
The readings for this week are:
2. A Deeper Dive into C Programming
For this week, focus on sections 2.1, 2.4, and 2.7.
For this week, review secions 3.1, 3.2, and 3.3 (ie. gdb and valgrind).
4. Binary and Data Representation
This should be review from Fundamentals of Computing and Logic Design, so skim this chapter and make sure you recognize most of this material.
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 reading09 # Create reading09 branch and check it out
$ cd reading09 # Go into reading09 folder
# Download Reading 09 Makefile
$ curl -LO https://www3.nd.edu/~pbui/teaching/cse.20289.sp25/static/txt/reading09/Makefile
# Download, build, and execute tests
$ make test
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 09 Quiz questions in your
reading09
branch:
Given the provided str_rev.c and Makefile, you are to do the following:
Modify Makefile to include a rule for the str_rev
program. Be
sure to use the CC
and CFLAGS
variables in your rule.
Modify str_rev.c so that it no longer has any uninitialized memory accesses, memory leaks, invalid memory acceses, or logical errors.
str_rev
must not modify the original string (hence the
const char *
). Consider the use of strdup.
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
To submit you work, follow the same process outlined in Reading 01:
#--------------------------------------------------
# BE SURE TO DO THE PREPARATION STEPS ABOVE
#--------------------------------------------------
$ cd reading09 # Go into reading09 folder
$ $EDITOR answers.json # Edit your answers.json file
$ ../.scripts/check.py # Check reading09 quiz
Checking reading09 quiz ...
Q1 1.40
Q2 0.15
Q3 0.15
Q4 0.30
Score 2.00 / 2.00
Status Success
$ git add answers.json # Add answers.json to staging area
$ git commit -m "Reading 09: Quiz" # Commit work
$ $EDITOR str_rev.c # Edit source code
$ make test # Build and Run tests
Checking reading09 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 1.00 / 1.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 09: Code" # Commit work
$ git push -u origin reading09 # Push branch to GitHub
If you collaborated with any other students, or received help from TAs or AI
tools on this assignment, please record this support in the README.md
in
the reading09
folder and include it with your Pull Request.
Remember to create a Pull Request and assign the appropriate TA from the Reading 09 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.