Garbage collection 3. If garbage collection were done just after the final sample output line is printed, explain how the basic mark-and-sweep algorithm would mark heap memory and what it could now sweep up. Since there are multiple instances of each type that is part of this answer, please be sure to uniquely distinguish each object you discuss.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Garbage collection

3. If garbage collection were done just after the final sample output line is printed, explain how the basic mark-and-sweep algorithm would mark heap memory and what it could now sweep up. Since there are multiple instances of each type that is part of this answer, please be sure to uniquely distinguish each object you discuss.

 

// Grades for a student

struct StuGrades {

string name;

int numQuiz;

int* scores;

};

 

// Read through file to find student with given

// If found, return pointer to new object with name and grades

StuGrades* readStudent(string nm, string filename) {

string nextName, buf;

ifstream in(filename);

while (in >> nextName) {

if (nm == nextName) {

StuGrades* result = new StuGrades;

result->name = nextName;

in >> result->numQuiz;

result->scores = new int[result->numQuiz];

for (int i = 0; i < result->numQuiz; i++) {

in >> result->scores[i];

}

in.close();

return result;

}

else // skip line

getline(in, buf);

}

in.close();

return NULL;

}

 

int main() {

string more = "y";

string nm;

 

while (more == "y") {

cout << "Enter name of student to lookup: ";

cin >> nm;

StuGrades* ans = readStudent(nm, "grades.txt");

if (ans != NULL)

cout << ans->name << ' ' << ans->numQuiz << endl;

for (int i = 0; i < ans->numQuiz; i++)

cout << ' ' << ans->scores[i];

cout << endl;

 

cout << "Keep searching? (y/n) ";

cin >> more;

}

return 0;

}

 

Here is the sample run:

Enter name of student to lookup: Lee

Lee 2

90 100

Keep searching? (y/n) y

Enter name of student to lookup: Jones

Jones 3

80 90 85

Keep searching? (y/n) y

Enter name of student to lookup: Lee

Lee 2

90 100 ß Q3: Just after this line is output

 

To help you visualize things, here is a grades.txt file that could give you the above output:

Lee 2 90 100

Jones 3 80 90 85

 

 

4. Using sample values with explanations of how the variables in the code are represented in memory, walk through an example of calling the guess() method for the Autoguess struct if it had used a “value receiver” instead of a pointer receiver. The goal is to show why it must be a pointer receiver.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education