10/13/2020 Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
Summary Quiz 2: Dynamic memory, Data structures, Strings
Due Oct 13 at 10pm Points 6 Questions 4
Available until Oct 13 at 10pm Time Limit 180 Minutes Allowed Attempts 3
Instruc ons
Extra a empt – due Oct 13, 10pm
Learning objec ves
This quiz will ask students to
Find memory-related errors and memory leaks in C programs with arrays
Find memory-related errors and memory leaks in C programs with data structures and/or strings Relate box-and-arrows diagrams to C code involving strings
Parameters
you can take 2 attempts
you earn the highest score of your attempts
you will only see your total score for the first attempt
a given attempt has a time limit of 3 hours (more time than needed)
NOTE: last time we accidentally made it 6 hours per attempt (yay math!). This time it really is 3 hours per attempt.
NOTE: typical due date for Summary Quizzes is Friday but we are extending this one to the following Monday
Constraints
take this quiz by yourself; treat it like an open-note, in-class exam open note/open book/ICON site
no interactive resources
Take the Quiz Again
A empt History
Attempt Time Score
https://uiowa.instructure.com/courses/143690/quizzes/213631 1/7
10/13/2020 Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
Attempt Time Score
KEPT Attempt 2 3 minutes 1.52 out of 6
LATEST Attempt 2 3 minutes 1.52 out of 6
Attempt 1 3 minutes 0 out of 6
Correct answers will be available Oct 13 at 10:05pm – Dec 18 at 12am.
Score for this attempt: 1.52 out of 6 Submitted Oct 5 at 8:16pm
This attempt took 3 minutes.
Question 1
0 / 0 pts
I completed this quiz according to the “Constraints” listed above. I understand that breaking these constraints is a violation of the CLAS Code of Academic Honesty (https://clas.uiowa.edu/students/handbook/academic-fraud-honor- code#:~:text=%22I%20pledge%20to%20do%20my,the%20Code%20of%20Aca , which will be prosecuted according to CLAS procedures for Undergraduate Academic Misconduct (https://clas.uiowa.edu/faculty/undergraduate-teaching-policies- resources/academic-misconduct) .
NOTE: If after your first attempt at this quiz, you are worried about doing well at the second attempt, please first seek help. See the Syllabus “Guide to Asking Questions”.
True False
https://uiowa.instructure.com/courses/143690/quizzes/213631 2/7
10/13/2020 Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
Partial
Question 2
1.33 / 2 pts
Consider the following program for printing the numbers 2 through 10. Mark ALL accurate descriptions of this implementation.
#include
void range(int start, int end, int *results) { int length = end – start;
results = malloc(sizeof(int) * length);
for (int i=start; i
#include
#include
#define MAX_STR_LEN 100
struct Node {
char *data;
struct Node *left;
struct Node *right;
};
struct Node *new_node(char *d) {
struct Node *r = malloc(sizeof(struct Node));
r->data = malloc(sizeof(char) * MAX_STR_LEN);
r->data = d;
r->left = NULL;
r->right = NULL;
return r;
}
void freeSubTree(struct Node *r) {
if (r->left != NULL) freeSubTree(r->left);
if (r->right != NULL) freeSubTree(r->right);
free(r->data);
free(r);
}
void printSubTree(struct Node *r) {
printf(“%s “, r->data);
if (r->left != NULL) printSubTree(r->left);
if (r->right != NULL) printSubTree(r->right);
10/13/2020
https://uiowa.instructure.com/courses/143690/quizzes/213631
Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
5/7
Partial
Memory leak: a Node hasn’t been freed by the end of the program Memory leak: a Node’s item array. Root cause of leak is in new_node()
Memory leak: a Node’s item array. Root cause of leak is in freeSubTree Invalid free: when free called on a struct Node*
Invalid free: when free called on a char*
No memory errors or leaks
Memory error in printSubTree(): when dereferencing r Memory error in freeSubTree(): when dereferencing r
Memory error in printf caused by invalid argument: r->data holds invalid address
}
int main() {
struct Node *root = new_node(“ABC”);
struct Node *left = new_node(“DEF”);
struct Node *right = new_node(“GHI”);
root->left = left;
root->right = right;
printSubTree(root);
freeSubTree(root);
}
Question 4
0.18 / 2 pts
Consider this program.
#include
10/13/2020 Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
#include
int main() {
char *go = 8 + malloc(sizeof(char) * 9); strcpy(go – 4, “bird”);
char *x = go;
}
What would be the box-and-arrow diagram right after the last line of main() executes? Give your answer by matching each labeled cell with a value. Assume the unlabeled array was created by the call to malloc.
go
A
B
C
D
E
F
G
H
J
K
x
A
B
C
D
arrow poin ng to the 1s
arrow poin ng to the 2n
arrow poin ng to the 3rd
arrow poin ng to the 4t
M
#include
https://uiowa.instructure.com/courses/143690/quizzes/213631 6/7
10/13/2020 Summary Quiz 2: Dynamic memory, Data structures, Strings: CS:2630:0AAA Fall20 Computer Organization
Quiz Score: 1.52 out of 6
E
F
G
H
J
K
M
‘i’
‘r’
‘d’
‘\0’ (null terminator)
‘\0’ (null terminator)
‘\0’ (null terminator)
‘b’
https://uiowa.instructure.com/courses/143690/quizzes/213631 7/7