全局结构数组,在程序中声明大小并在不通过的情况下全局使用?

时间:2018-11-26 17:34:33

标签: c arrays struct malloc parameter-passing

仅就上下文而言,我将缓存模拟器作为C项目进行制作。我需要声明一个结构的全局双精度数组,在程序中设置该全局结构数组的大小,然后稍后在项目中使用该全局数组。

这是我所拥有的东西的简化版本:

// Declare globals
int mapping = 0;
int offset = 0;
int index = 0;

char* allocation;
char* writePolicy;

struct cache {
    int validBit;
    int dirtyBit;
    unsigned int tag;
    unsigned int nextToReplace;
    };

void analyzeParameters(char* line, int lineNum) {

switch(lineNum) {
        case 1:
            mapping = atoi(line);
        case 2:
            offset = atoi(line);
        case 3:
            index = atoi(line);
        case 4:
            allocation = malloc(4);
            strcpy(allocation, line);
        case 5:
            writePolicy = malloc(4);
            strcpy(writePolicy, line);
    }

}

setupCache() {
int numSets = 1 << index;

struct cache cache[mapping][numSets];

printf("Declared cache struct with size %d ", numSets);
printf("num of ways %d\n", mapping);

// initialize bits in cache to 0

int j;
int i;

for (j = 0; j < mapping; j++) { 
    for (i = 0; i < numSets; i++) {
        cache[j][i].validBit = 0;
        cache[j][i].dirtyBit = 0;
    }
}

}

void hitChecker() {

for (d = 0; d < mapping; d++) {
    if (cache[d][index].validBit == 1) {
        if (cache[d][index].tag == tag) { 
            //found a hit

            if (type == "r") {
            // hit with a read instruction.
            rhits++;


            }
            else if (type == "w") {
            // hit with a write instruction
            whits++;

            }
        }

        else {
        // tag in cache index is not equal to tag being checked

            if (type == "r") {
            // missed with a read instruction.
            rmisses++;

            }
            else if (type == "w") {
            // missed with a write instruction
            wmisses++;

            }
        }
    }
    else {
    //cache is not valid
    printf("Cache has not been validated");
    }
}

void main(int argc, char**argv) {

analyzeParameters(passInEachLineOfFile, this works not important);

setupCache();

hitChecker();

}

在我尝试利用缓存结构之前,这一直有效。我全局声明它,在setUpCache中设置大小,然后在另一个方法中,我想使用全局声明的double数组。有没有办法可以在全局范围内使用它,还是必须通过方法参数传递该结构?

2 个答案:

答案 0 :(得分:1)

要使用运行时确定大小的全局缓存结构,请使用:

int mapping, numsets;
struct cache **cache;

和:

void init(int nmaps, int nsets)
{
    cache=malloc(nmaps*sizeof(struct cache *));
    for (int i=0;i<nmaps;i++)
        cache[i]=malloc(nsets*sizeof(struct cache));
    mapping= nmaps;
    numsets= nsets;
}

答案 1 :(得分:0)

如果要将cache用作全局变量,请在全局范围内声明它,并在使用之前进行声明。

struct cache {
    int validBit;
    int dirtyBit;
    unsigned int tag;
    unsigned int nextToReplace;
    };

struct cache cache[mapping][numSets];

如果可以避免的话,将变量放在全局范围内不是一个好主意。

在这里,要“封装” 缓存功能,我建议在单独文件中使用一个 static 变量,同时保留所有缓存-相关功能,以使其在“文件”范围内。由于mappingoffsetindex是来自缓存函数外部的参数,因此请将它们作为参数传递。

// cache-lib.c

// Declare the struct in the c file, it's only needed here
struct cache {
        int validBit;
        int dirtyBit;
        unsigned int tag;
        unsigned int nextToReplace;
        };

static struct cache cache[mapping][numSets];

// Stores below variable here, in the "file" scope.
static mapping, offset, index;

// Below cache functions. Declare them an includable header.

/// setupCache() definition. Since it needs mapping, offset and index which
/// are retrieved from the outside, you can pass them as parameter.

void setupCache(int mapping, int offset, int index) {
  //// [...]
}

/// hitChecker. Maybe you need here to store extra variables
/// as "file-scope" ones like index and mapping
void hitChecker() {
  //// [...]
}

最后,您将在需要缓存库的任何地方使用的标头:

// cache-lib.h

void setupCache(int mapping, int offset, int index);
void hitChecker();

您也可以将其包括在cache-lib.c中,以免打扰与函数声明顺序有关的问题。