Admin
===========================Q1========================
2021_sep_mt
The following code contains three modules. Student objects should be reference counted.
This code does this correctly in some places, but makes several mistakes. Fix these mistakes
by adding calls to rc_keep_ref and/or rc_free_ref and by making any other changes
that are required so that the resulting code uses reference counting correctly, according to
the guidelines we have discussed in class and so that the code is free of memory leaks, dangling
pointers, and unnecessary coupling.
code optional view link: https://onlinegdb.com/mmtNypyZ1b
/*******************************
* Student Module
*******************************/
/***************
* Private
*/
struct Student {
int id;
int grade;
} *students[2] = {NULL, NULL};
int cur = 0;
double scale = 1.2;
/***************
* Public
*/
void Student_add(int id, int grade) {
students[cur] = rc_malloc(sizeof(struct Student));
students[cur]->id = id;
students[cur]->grade = grade * scale;
cur=(cur+1)%2; //i.e.,(cur+1)mod2...itwrapsaround:0,1,0,1etc.
}
struct Student *Student_get(int id) {
for (int i=0; i<2; i++)
if (students[i] != NULL && students[i]->id == id) {
return students[i];
}
return NULL;
}
void Student_cleanup() {
for (int i=0; i<2; i++) {
if (students[i] != NULL) {
free(students[i]);
students[i] = NULL;
}
}
}
/*******************************
* Top Module
*******************************/
/***************
* Private
*/
struct Student *top = NULL;
/***************
* Public
*/
void Top_saveTop(struct Student *student) {
if (top == NULL || student->grade > top->grade) {
if (top != NULL) {
rc_free_ref(top);
}
top = student;
rc_keep_ref(top);
}
}
int Top_getTopGrade() {
if (top != NULL) {
return top->grade;
} else
return -1;
}
void Top_cleanup() {
if (top != NULL) {
rc_free_ref(top);
top = NULL;
}
}
/*******************************
* Main Module
*******************************/
struct Student *Main_add(int id, int grade) {
Student_add(id, grade);
struct Student *s = Student_get(id);
Top_saveTop(s);
return s;
}
int Main_run(int *ids, int *grades) {
struct Student *s0 = Main_add(ids[0], grades[0]);
struct Student *s1 = Main_add(ids[1], grades[1]);
struct Student *s2 = Main_add(ids[2], grades[2]);
int top = Top_getTopGrade();
Student_cleanup();
Top_cleanup();
return top / ((s0->grade + s1->grade + s2->grade) / 3);
}
===========================Q2========================
2021_Jan_mt
(10marks) Reference Counting. Consider the following program, implementing classlists,
which are objects that contain lists of distinct, dynamically-allocated student objects that
should be managed using reference counting. A call to rc_malloc has been added for you;
recall that rc_malloc sets the allocated object’s reference count to 1.
7a Add calls to rc_keep_ref and rc_free_ref to correctly implement reference counting
for this program, and implement the delete_classlist procedure, such that there are no memory
errors and all objects are freed by the time main completes. Do not add or remove any other code.
Use the comments as a guideline to determine how functions should handle reference counts.
code optional view link: https://onlinegdb.com/7KvXRsPTs
/* Classlist structure. DO NOT MODIFY. =*/
struct classlist {
struct student sstudents; // all students in the classlist
int max_students; // maximum number of students
int num_students; // current number of students
};
/* Student structure. DO NOT MODIFY. x/
struct student {
int sID; // student number
int avg_grade;
};
struct student stop_student; // this is a global pointer
/*Create a new student. DO NOT MODIFY. */
struct student screate_student (int sID, int avg_grade) {
struct student *s = rc_malloc(sizeof(struct student));
s->sID = sID;
s->avg_grade = avg_grade;
return s;
}
/* Create a classlist of specified maximum size. DO NOT MODIFY. */
struct classlist create_classlist (int max_students) {
struct classlist *c = malloc(sizeof(struct classlist));
c->students = malloc(sizeof (struct student x) * max_students);
c->max_students = max_students;
c->num_students = 0;
return c;
}
/* Find a student if they are in the classlist; return NULL otherwise.
The student is not removed from the classlist.*/
struct student* get_student (struct classlist *c, int sID) {
for(int i=0; i < c->max_students; i++) {
if ((c->students[i]->sID) == sID) {
returtn c->students[i];
}
}
return NULL;
}
/* Add a student to a classlist if they are not already there
and if there is space available in the list. */
void add_student (struct classlist xc, struct student * s) {
if (c->num_students == c->max_students) return;
struct student *stu = get_student (c, s->sID);
if (stu != NULL) {
return;
}
c->students [c->num_students] = s;
c->num_studentS5++;
}
/* Update the top student by comparison to a supplied classlist. */
void update_top_student (struct classlist * c) {
for (int i=0; i < c->num_students; i++) {
if (top_student->avg_grade < c->students[i]->avg_grade) {
top_student = c->students[i];
}
}
}
/* Delete the specified classlist. IMPLEMENT THIS. x/
void delete_classlist (struct classlist *c) {
//todo!!!
}
int main() {
top_student = create_student (00000000, 0);
struct classlist *cs213 = create_classlist (600);
struct classlist *cs221 = create_classlist(600);
struct student* s1 = create_student (42424242, 42);
struct student* s2 = create_student (98765432, 87);
struct student* s3 = create_student (31415926, 54);
add_student (cs213, s1);
add_student (cs221, s1);
add_student (cs213, s2);
add_student (cs221, s3);
struct student x s4 = get_student (cs213, 98765432);
struct student * s5 = s4;
add_student (cs221, s4);
update_top_student (cs213);
update_top_student (cs221);
printf("%d, %d\n", top_student->sID, top_student->avg_grade);
delete_classlist (cs213);
delete_classlist (cs221);
}
7b With your implementation from the previous part, give the reference counts for the following
objects right before delete_classlist is called for the first time:
*s1:
*s2:
*s3:
===========================Q3========================
===========================Q4========================
===========================Q5========================
===========================Q6========================
===========================Q7========================
===========================Q8========================
===========================Q9========================
===========================Q10========================
===========================Q11========================
===========================Q12========================
===========================Q13========================