{ "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\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n \n \n \n \n \n\n \n
CodeStackHeapData
double GD = 3.14;____ ( 1)____ ( 2)____ ( 3)
int a[] = {4, 6, 6, 3, 7};____ ( 4)____ ( 5)____ ( 6)
char *sp = \"Happy Kid\";____ ( 7)____ ( 8)____ ( 9)
char sa[] = \"Frug\";____ (10)____ (11)____ (12)
Block b = {0};____ (13)____ (14)____ (15)
Point p0 = {0, 0};____ (16)____ (17)____ (18)
Point *p1 = NULL;____ (19)____ (20)____ (21)
Point *p2 = malloc(sizeof(Point));____ (22)____ (23)____ (24)
Point *p3 = malloc(10*sizeof(Point));____ (25)____ (26)____ (27)
Point **p4 = malloc(10*sizeof(Point *));____ (28)____ (29)____ (30)
\n\n

Assume this program is compiled on a 64-bit Linux machine (ie. the\nstudent machines).

\n" }, "q3": { "type": "multiple", "question": "\n

Fix 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": { "noleak": "There are no memory leaks.", "nouninit": "There are no uninitialized memory accesses.", "leak": "There is a memory leak.", "uninit": "There are uninitialized memory accesses.", "noinvalid": "There are no invalid memory accesses.", "invalid": "There are invalid memory accesses." } }, "q2": { "type": "multiple", "question": "\n

Build 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": { "malloc": "The program segfaults on the line char *t = malloc(strlen(s)).", "puts": "The program segfaults on the line puts(t).", "tnull": "The program segfaults because t is NULL.", "slong": "The program segfaults because s is too long.", "iinit": "To fix the segfault, we must set int i = 0 in the for loop.", "tfree": "To fix the segfault, we must call free(t).", "toupper": "The program segfaults on the line *c = toupper(*c).", "sfree": "To fix the segfault, we must call free(s).", "tlong": "The program segfaults because t is too long.", "snull": "The program segfaults because s is NULL.", "strncpy": "The program segfaults on the line strncpy(t, s, strlen(t)).", "iargc": "To fix the segfault, we must check i < argc in the for loop." } } }