You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C语言实现LRU算法并统计内存读写次数时的编译错误排查求助

解决LRU算法封装中的编译错误

我帮你逐个排查这些编译错误,问题其实都集中在两个关键点上,修复起来很简单:

错误1:重复定义变量temp

在newQNode函数里,你不小心重复声明并分配了temp变量,C语言不允许在同一作用域内重复定义同名变量:

// 原错误代码片段
QNode* newQNode(unsigned pageNumber) {
    QNode* temp = (QNode*)malloc(sizeof(QNode));
    temp->pageNumber = pageNumber;
    QNode* temp = (QNode*)malloc(sizeof(QNode)); // 多余的重复定义
    temp->pageNumber = pageNumber;
    temp->prev = temp->next = NULL;
    return temp;
}

修复方法:删掉第二行重复的malloc和赋值语句,只保留一次内存分配即可。

错误2:代码块脱离函数作用域

你的Enqueue函数提前闭合了花括号,导致后续的核心逻辑代码跑到了全局作用域中。全局作用域无法访问函数参数pageNumber,也不能直接执行赋值、条件判断这类语句,因此引发了一系列连锁错误:

// 原错误代码片段
void Enqueue(Queue* queue, Hash* hash, unsigned pageNumber) {
    if (AreAllFramesFull(queue))
        hash->array[queue->rear->pageNumber] = NULL;
    deQueue(queue);
} // 这里错误地闭合了函数花括号

// 以下代码本该属于Enqueue函数内部
QNode* temp = newQNode(pageNumber);
temp->next = queue->front;
...

修复方法:删掉这个多余的花括号,将所有逻辑代码都包含在Enqueue函数的作用域内。


完整修正后的代码

#include <stdio.h>
#include <stdlib.h>

typedef struct QNode {
    struct QNode *prev, *next;
    unsigned pageNumber; // the page number stored in this QNode
} QNode;

typedef struct Queue {
    unsigned count; // Number of filled frames
    unsigned numberOfFrames; // total number of frames
    QNode *front, *rear;
} Queue;

typedef struct Hash {
    int capacity; // how many pages can be there
    QNode** array; // an array of queue nodes
} Hash;

QNode* newQNode(unsigned pageNumber) {
    QNode* temp = (QNode*)malloc(sizeof(QNode));
    temp->pageNumber = pageNumber;
    temp->prev = temp->next = NULL;
    return temp;
}

Queue* createQueue(int numberOfFrames) {
    Queue* queue = (Queue*)malloc(sizeof(Queue));
    queue->count = 0;
    queue->front = queue->rear = NULL;
    queue->numberOfFrames = numberOfFrames;
    return queue;
}

Hash* createHash(int capacity) {
    Hash* hash = (Hash*)malloc(sizeof(Hash));
    hash->capacity = capacity;
    hash->array = (QNode**)malloc(hash->capacity * sizeof(QNode*));
    int i;
    for (i = 0; i < hash->capacity; ++i)
        hash->array[i] = NULL;
    return hash;
}

int AreAllFramesFull(Queue* queue) {
    return queue->count == queue->numberOfFrames;
}

int isQueueEmpty(Queue* queue) {
    return queue->rear == NULL;
}

void deQueue(Queue* queue) {
    if (isQueueEmpty(queue))
        return;
    if (queue->front == queue->rear)
        queue->front = NULL;
    QNode* temp = queue->rear;
    queue->rear = queue->rear->prev;
    if (queue->rear)
        queue->rear->next = NULL;
    free(temp);
    queue->count--;
}

void Enqueue(Queue* queue, Hash* hash, unsigned pageNumber) {
    if (AreAllFramesFull(queue)) {
        hash->array[queue->rear->pageNumber] = NULL;
        deQueue(queue);
    }

    QNode* temp = newQNode(pageNumber);
    temp->next = queue->front;
    if (isQueueEmpty(queue))
        queue->rear = queue->front = temp;
    else { // Else change the front
        queue->front->prev = temp;
        queue->front = temp;
    }
    // Add page entry to hash also
    hash->array[pageNumber] = temp;
    // increment number of full frames
    queue->count++;
}

// This function is called when a page with given 'pageNumber' is referenced
// from cache (or memory). There are two cases:
// 1. Frame is not there in memory, we bring it in memory and add to the front
// of queue
// 2. Frame is there in memory, we move the frame to front of queue
void ReferencePage(Queue* queue, Hash* hash, unsigned pageNumber) {
    QNode* reqPage = hash->array[pageNumber];
    // the page is not in cache, bring it
    if (reqPage == NULL)
        Enqueue(queue, hash, pageNumber);
    // page is there and not at front, change pointer
    else if (reqPage != queue->front)
    {
        // Unlink requested page from its current location
        // in queue.
        reqPage->prev->next = reqPage->next;
        if (reqPage->next)
            reqPage->next->prev = reqPage->prev;
        // If the requested page is rear, then change rear
        // as this node will be moved to front
        if (reqPage == queue->rear)
        {
            queue->rear = reqPage->prev;
            queue->rear->next = NULL;
        }
        // Put the requested page before current front
        reqPage->next = queue->front;
        reqPage->prev = NULL;
        // Change prev of current front
        reqPage->next->prev = reqPage;
        // Change front to the requested page
        queue->front = reqPage;
    }
}

// Driver program to test above functions
int main() {
    // Let cache can hold 4 pages
    Queue* q = createQueue(4);
    // Let 10 different pages can be requested (pages to be
    // referenced are numbered from 0 to 9
    Hash* hash = createHash(10);
    // Let us refer pages 1, 2, 3, 1, 4, 5
    ReferencePage(q, hash, 1);
    ReferencePage(q, hash, 2);
    ReferencePage(q, hash, 3);
    ReferencePage(q, hash, 1);
    ReferencePage(q, hash, 4);
    ReferencePage(q, hash, 5);
    // Let us print cache frames after the above referenced pages
    printf("%d ", q->front->pageNumber);
    printf("%d ", q->front->next->pageNumber);
    printf("%d ", q->front->next->next->pageNumber);
    printf("%d ", q->front->next->next->next->pageNumber);
    return 0;
}

另外,我还顺手给Enqueue里的if加了花括号,修正了一处拼写错误(rquested改为requested),这样代码结构更清晰,也避免后续加代码时出现逻辑错误。

内容的提问来源于stack exchange,提问作者jega

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:38:13