Everyone:
Next week, we will return to the idea of virtualization and consider how the operating system abstracts physical memory for each process.
For this reading assignment, you are to read about address spaces, free-space management, address translation, and segmentation, and submit your responses to the Reading 07 Quiz.
The readings for next week are:
Once you have done the readings, answer the following Reading 07 Quiz questions:
For this week, you are to identify common memory problems. Given the following five snippets of code:
// Snippet A
for (int i = 1; i < argc; i++) {
char *copy = strdup(argv[i]);
puts(copy);
}
// Snippet B
char buffer[BUFSIZ];
puts(buffer);
// Snippet C
char *s = "string";
*s = 'S';
// Snippet D
char *s = "string";
free(s);
// Snippet E
char *buffer = calloc(1, strlen(argv[0]));
strcat(buffer, argv[0]);
puts(buffer);
free(buffer);
Identify if the code snippet demonstrates one of the following errors:
segfault
buffer overflow
uninitialized read
memory leak
dangling pointer
double free
invalid free
In your program.c
, simply use puts
to print out the error:
int main(int argc, char *argv[]) {
puts("segfault");
...
puts("invalid free");
return 0;
}
To submit you work, follow the same process outlined in Reading 01:
$ git switch master # Make sure we are in master branch
$ git pull --rebase # Make sure we are up-to-date with GitLab
$ git checkout -b reading07 # Create reading07 branch and check it out
$ cd reading07 # Go into reading07 folder
$ $EDITOR answers.json # Edit your answers.json file
$ ../.scripts/check.py # Check reading07 quiz
Checking reading07 quiz ...
Q01 0.20
Q02 0.15
Q03 0.40
Q04 0.50
Q05 0.15
Q06 0.30
Q07 0.15
Q08 0.15
Score 2.00 / 2.00
Status Success
$ git add answers.json # Add answers.json to staging area
$ git commit -m "Reading 07: Quiz" # Commit work
$ $EDITOR program.c # Edit your program.c file
$ make test-program # Check reading03 program
Testing reading07 program...
Score 2.00 / 2.00
Status Success
$ git add Makefile # Add Makefile to staging area
$ git add program.c # Add program.c to staging area
$ git commit -m "Reading 07: Code" # Commit work
$ git push -u origin reading07 # Push branch to GitHub
Once you have committed your work and pushed it to GitHub, remember to create a pull request and assign it to the appropriate teaching assistant from the Reading 07 TA List.