我想知道破解此代码的正确方法,因为我面临许多随机问题。但是,这条代码正在工作,因为我首先调用~CacheFrame()
而不是nullptr
pageMap
~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 to 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();
for (auto it: pageMap) {
cout << "Deleting " << it.first << " from 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};
for (auto it: array) {
cache.accessPage(it);
}
return 0;
}
print("Leaving definition : " + func._name_)
答案 0 :(得分:0)
C ++:什么是破坏此代码的最佳方法?
你的意思是释放资源。
如果是unordered_map
正确行事的方法不是这样做。 unordered_map
会在资源被自动分配时自动释放资源。
除非您使用new
分配值,否则不会delete
。
如果是linked list
~CacheFrame()
{
Node *temp = head->next, *temp2;
while(temp != NULL)
{
temp2 = temp;
temp = temp->next;
delete temp2;
}
}