{ "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 | \nData | \nHeap | \nStack | \n \n \n
---|---|---|---|
double GD = 3.14; | \n____ ( 1) | \n____ ( 2) | \n____ ( 3) | \n
int a[] = {5, 7, 4}; | \n____ ( 4) | \n____ ( 5) | \n____ ( 6) | \n
char *sp = \"Video Games\"; | \n____ ( 7) | \n____ ( 8) | \n____ ( 9) | \n
char sa[] = \"Runescape\"; | \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(5*sizeof(Point)); | \n____ (25) | \n____ (26) | \n____ (27) | \n
Point **p4 = malloc(5*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_rev.c with the appropriate compiler flags and then\nrun it using gdb:
\n\n\n$ gcc -Wall -g -std=gnu99 -o str_rev str_rev.c\n\n$ gdb ./str_rev\n...\n(gdb) run old school\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).", "copy": "The program segfaults on the line *w++ = *r--.", "strcpy": "The program segfaults on the line strncpy(t, s, strlen(s)).", "strlen": "The program segfaults on the line char *t = malloc(strlen(s)).", "nulls": "The program segfaults because s is NULL.", "nullt": "The program segfaults because t is NULL.", "longs": "The program segfaults because s is too long.", "longt": "The program segfaults because t is too long.", "frees": "To fix the segfault, we must call free(s).", "freet": "To fix the segfault, we must call free(t).", "inti": "To fix the segfault, we must set int i = 0 in the for loop.", "argc": "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_rev old school\n\n\n
Which of the following statements are true (select all that apply)?
\n", "responses": { "nouninits": "There are no uninitialized memory accesses.", "uninits": "There are uninitialized memory accesses.", "noleaks": "There are no memory leaks.", "leaks": "There is a memory leak.", "noinvalids": "There are no invalid memory accesses.", "invalids": "There are invalid memory accesses." } } }