{ "q1": { "type": "blank", "question": "\n
Given allocations.c, fill in the table below to identify how\nmuch memory (in bytes) is allocated in each memory segment (ie.\nstack, heap, data) for the corresponding variable\ndeclaration:
\n\nCode | \nStack | \nHeap | \nData | \n \n \n
---|---|---|---|
double GD = 3.14; | \n____ ( 1) | \n____ ( 2) | \n____ ( 3) | \n
int a[] = {4, 6, 6, 3, 7}; | \n____ ( 4) | \n____ ( 5) | \n____ ( 6) | \n
char *sp = \"Happy Kid\"; | \n____ ( 7) | \n____ ( 8) | \n____ ( 9) | \n
char sa[] = \"Frug\"; | \n____ (10) | \n____ (11) | \n____ (12) | \n
Block b = {0}; | \n____ (13) | \n____ (14) | \n____ (15) | \n
Point p0 = {0, 0}; | \n____ (16) | \n____ (17) | \n____ (18) | \n
Point *p1 = NULL; | \n____ (19) | \n____ (20) | \n____ (21) | \n
Point *p2 = malloc(sizeof(Point)); | \n____ (22) | \n____ (23) | \n____ (24) | \n
Point *p3 = malloc(10*sizeof(Point)); | \n____ (25) | \n____ (26) | \n____ (27) | \n
Point **p4 = malloc(10*sizeof(Point *)); | \n____ (28) | \n____ (29) | \n____ (30) | \n
Assume this program is compiled on a 64-bit Linux machine (ie. the\nstudent machines).
\n" }, "q2": { "type": "multiple", "question": "\nBuild str_title.c with the appropriate compiler flags and then\nrun it using gdb:
\n\n\n$ gcc -Wall -std=gnu99 -g -gdwarf-2 -o str_title str_title.c\n\n$ gdb ./str_title\n...\n(gdb) run\n...\nProgram received signal SIGSEGV, Segmentation fault.\n(gdb) bt\n...\n\n\n
Which of the following statements are true (select all that apply)?
\n", "responses": { "puts": "The program segfaults on the line puts(t).", "toupper": "The program segfaults on the line *c = toupper(*c).", "strncpy": "The program segfaults on the line strncpy(t, s, strlen(t)).", "malloc": "The program segfaults on the line char *t = malloc(strlen(s)).", "slong": "The program segfaults because s is too long.", "tlong": "The program segfaults because t is too long.", "snull": "The program segfaults because s is NULL.", "tnull": "The program segfaults because t is NULL.", "sfree": "To fix the segfault, we must call free(s).", "tfree": "To fix the segfault, we must call free(t).", "iinit": "To fix the segfault, we must set int i = 0 in the for loop.", "iargc": "To fix the segfault, we must check i < argc in the for loop." } }, "q3": { "type": "multiple", "question": "\nFix the segmentation fault in Q2, rebuild the program, and then\nuse valgrind to run the program:
\n\n\n$ valgrind --leak-check=full ./str_title \"harry potter\"\n\n\n
Which of the following statements are true (select all that apply)?
\n", "responses": { "nouninit": "There are no uninitialized memory accesses.", "uninit": "There are uninitialized memory accesses.", "noleak": "There are no memory leaks.", "leak": "There is a memory leak.", "noinvalid": "There are no invalid memory accesses.", "invalid": "There are invalid memory accesses." } }, "q4": { "type": "blank", "question": "\nFill in the blanks for the following program: walk.c
\n\n
\n/* walk.c */\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#include <sys/stat.h>\n#include <dirent.h>\n#include <unistd.h>\n\nint main(int argc, char *argv[]) {\n /* Open directory handle */\n DIR *d = ____(\".\"); /* 1 */\n if (!d) {\n fprintf(stderr, \"%s\\n\", ____(errno)); /* 2 */\n return EXIT_FAILURE;\n }\n\n /* For each directory entry, check if it is a file, and print out the its\n * name and file size */\n for (struct dirent *e = ____(d); e; e = ____(d)) { /* 3 & 4 */\n /* Skip current directory and parent directory */\n if (strcmp(____, \".\") == 0 || strcmp(____, \"..\") == 0) { /* 5 & 6 */\n continue;\n }\n\n /* Skip non-regular files */\n if (____ != DT_REG) { /* 7 */\n continue;\n }\n\n /* Get file meta-data */\n struct stat s;\n if (____(____, &s) < 0) { /* 8 & 9 */\n fprintf(stderr, \"%s\\n\", ____(errno)); /* 10 */\n continue;\n }\n\n /* Display file name and size */\n printf(\"%s %lu\\n\", ____, ____); /* 11 & 12 */\n }\n\n /* Close directory handle */\n ____(d); /* 13 */\n return EXIT_SUCCESS;\n}\n\n\n
Note, that this program performs the equivalent of the following Python code:
\n\n\nimport os\n\nwith os.scandir('.') as entries:\n for entry in entries:\n if entry.is_file():\n print(entry.name, entry.stat().st_size)\n\n\n
Your program should display something like this:
\n\n\nwalk.c 1161\nwalk 22896\nMakefile 565\nREADME.md 22\nwalk.py 136\n\n" } }