c++11 - C++: What is best way to destruct this code? -
i want know proper way of destructing code i'm facing many random issues. however, piece of code working i'm calling ~cacheframe()
first , assinging nullptr
pagemap
inside ~lrucache()
#include <iostream> #include <unordered_map> #include <list> using namespace std; struct node { int pagenumber; node *next; node *prev; node(int pagenumber, node *prev, node *next) { this->pagenumber = pagenumber; this->next = next; this->prev = prev; } }; class cacheframe { const size_t maxsize; int size; node *head, *tail; public: cacheframe(int maxsize) : maxsize(maxsize) { size = 0; head = tail = nullptr; } ~cacheframe() { cout << "cacheframe destructor called" << endl; tail = nullptr; while (head) { node *temp = head->next; head->next = nullptr; head->prev = nullptr; delete head; head = temp; } } void movepagetofront(node* page) { if (page == nullptr || page == head) { return; } if (page == tail) {// point tail second last element tail = tail->prev; tail->next = nullptr; } page->prev->next = page->next; if (page->next) page->next->prev = page->prev; page->next = head; head->prev = page; head = page; } node* insertpage(int pagenumber) { node *page = new node(pagenumber, nullptr, nullptr); if (!head) { head = tail = page; size = 1; return head; } if (size < maxsize) { size++; } else { node *temp = tail; tail = tail->prev; tail->next = nullptr; delete temp; } page->next = head; head->prev = page; head = page; return page; } size_t getcurrentsize() { return size; } size_t getmaxsize() { return maxsize; } int getlastpagenumber() { return tail->pagenumber; } void printframe() { node *start = head; cout << "frame" << endl; while (start) { cout << "-----" << endl; cout << "| " << start->pagenumber << " |" << endl; start = start->next; } cout << "-----"<< endl; } }; class lrucache { const size_t size; cacheframe frame; unordered_map<int, node*> pagemap; public: lrucache(int size): size(size), frame(cacheframe(size)) {} ~lrucache() { cout << "lrucache destructor called" << endl; frame.~cacheframe(); (auto it: pagemap) { cout << "deleting " << it.first << " map" << endl; it.second = nullptr; pagemap.erase(it.first); // if (it.second) { // it.second->next = nullptr; // it.second->prev = nullptr; // delete it.second; // } } } void accesspage(int pagenumber) { cout << "access page : " << pagenumber << endl; if (pagemap.find(pagenumber) != pagemap.end()) {// page hit cout << "page hit" << endl; frame.movepagetofront(pagemap[pagenumber]); } else {//page miss cout << "page miss" << endl; if (frame.getmaxsize() == frame.getcurrentsize()) { pagemap.erase(frame.getlastpagenumber()); } pagemap[pagenumber] = frame.insertpage(pagenumber); } frame.printframe(); } }; int main() { lrucache cache = lrucache(3); int array[] = {4,2,1,1,4,3,7,8,3}; (auto it: array) { cache.accesspage(it); } return 0; }
c++: best way destruct code?
you mean free resources.
in case of unordered_map
the way right not it. unordered_map
automatically release resources when it's destroyed allocated automatically.
unless allocated values new
, don't delete
them.
in case of linked list
~cacheframe() { node *temp = head->next, *temp2; while(temp != null) { temp2 = temp; temp = temp->next; delete temp2; } }
Comments
Post a Comment