繁体   English   中英

将值赋给结构指针内部的结构指针数组中的索引

[英]Assigning value to an index in a struct pointer array inside of a struct pointer

我目前在将NODE类型对象分配给INV_PAGE_TABLE结构内部的NODE*数组时INV_PAGE_TABLE

结构如下:

typedef struct node {
    int pid;
    int p;
    int offset;
    unsigned TAG;
} NODE;

typedef struct invTablePage {
    NODE *pageTable;
    int frameSize;
    int currentSize;
    int totalSize;
    int oldest;
    int maxIndex;
} INV_PAGE_TABLE;

invTablePage的分配如下:

void initInverted(INV_PAGE_TABLE *invTable, int memSize, int frameSize) {
    //Malloc inverted page table
    invTable = malloc(sizeof(struct invTablePage));
    //Save frameSize
    invTable->frameSize = frameSize;
    //Save totalSize
    invTable->totalSize = memSize / frameSize - 1;
    //Save currentSize
    invTable->currentSize = 0;
    //Set oldest
    invTable->oldest = 0;
    //Malloc array inside of page table
    invTable->pageTable = malloc(sizeof(NODE) * invTable->totalSize);
}

最后是调用细分错误的方法

void addToPageTable(struct invTablePage *invTable, NODE *node) {
    NODE tempNode;
    //If pageTable is not full
    int currentSize = invTable->currentSize;

    if (invTable->currentSize != invTable->totalSize) {
        //Add Entry at index of currentSize

        /*FOLLOWING LINE CRASHES PROGRAM*/
        invTable->pageTable[currentSize] = node;

        //Update currentSize
        invTable->currentSize++;
        //If pageTable is full
    } else {
        //Set temp to oldest
        tempNode = invTable->pageTable[invTable->oldest];
        //Set oldest to node
        invTable->pageTable[invTable->oldest] = *node;
    }
}

请注意,例如,在数组[10]中,索引的范围是0到9

因此,您的总大小应为invTable-> totalSize = memSize / frameSize; 并且currentSize不应超过invTable-> totalSize-1。但是我认为您必须分配memSize / frameSize而不是memSize / frameSize-1 ...

我不确定我的答案是否正确,请尝试打印...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM