结构数组打印和修改

时间:2015-11-25 23:18:48

标签: c arrays file struct file-processing

所以我有一个家庭作业,使用c程序从文本文档中读取地址,并检查它是否被击中或错过   当我执行打印索引和值的最终过程时,会出现垃圾编号,但我不知道原因 它总是打印出这样的东西:

printinfo index loop: 8
printinfo index loop: 779318117

这是绝对错误的,因为索引应该是0-7,而不是8或这个时髦的数字

以下是代码:

    /*
     *This is the program for lab assignment3 the Cache Simulator
     *
     * Format of cache:
     * Since the cache block size is 4 words,
     * 4 words = 4 *4bytes = 16 bytes
     * So the offset will be 4 bits (0-3)
     * We know the cache have 8 entries
     * 2 ^3 = 8
     * So the index will be 3 bits (4-6)
     * Since this is a 32 bit address
     * Tag = 31 - 4 - 3 = 31-7 = 24 bits (7-30)
     * The format of each cache line will be:
     *  31-8   5-7     1-4       0
     * | Tag | |index| |offset| |valid|
     */

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define BLOCK_SIZE 16  // each block store 4 words
    #define FILE_NAME "address.txt"
    #define NUMBER_OF_ADDRESS 10
    #define OFFSET_BIT 4
    #define INDEX_BIT 3

    struct Cache_file
    {
    int tag;
    int valid;
    unsigned int index;
    int dataLow;
};

//function prototypes
void printState (struct Cache_file cacheLine[] , int totalCacheLineNum);
void readFile(FILE *filePtr, char* inFileName, int accessTime,
              int *address, struct Cache_file cacheLine[]);

void checkHit(int address[], struct Cache_file cacheLine[], int accessTime);
void printHeader();
void printInfo(int hitNum, int missNum, float hitRate, float missRate, int totalAccess, struct Cache_file cacheLine[]);


//functions to print miss rate and hit rate
void printInfo(int hitNum, int missNum, float hitRate, float missRate,int totalAccess, struct Cache_file cacheLine[])
{
    printf("The total hit time is: %d\n", hitNum);
    printf("The total miss time is: %d\n", missNum);
    printf("The hit rate is: %0.3f\n", hitRate);
    printf("The miss rate is: %0.3f\n", missRate);
    printHeader();
    int i=0;
    for(i;i<totalAccess; i++)
    {
        printf("printinfo index loop: %d\n", cacheLine[i].index);
    }
}

// the header
void printHeader()
{
    printf("<Cache State>: \n");
    printf("Index      Tag  Valid\t Data\n");
    printf("-------------------------------------------------------\n");
}


void checkHit(int *address, struct Cache_file cacheLine[], int accessTime)
{
//test
    int j =0;
    for(j;j<accessTime;j++)
    {
        printf("the address in checkHit: %.6lX\n" ,address[j]);
    }


    int missNum = 0;
    int hitNum = 0;

    float hitRate = 0.0;
    float missRate = 0.0;

    // tag =  address / 2 ^ (offset+index bits)
    int exp = OFFSET_BIT+INDEX_BIT;
    int dn = pow(2,exp);
    int indx = 0;
    int tag = 0;

    int i=0;
    for(i;i<accessTime;i++)
    {

        indx = (address[i]/BLOCK_SIZE)%8; //0-7
        printf("index in checkHit loop: %d\n", indx);
 //       printf("index cacheline[%d]: %d \n",i,cacheLine[i].index);
        tag= (address[i] /dn);

        if(cacheLine[indx].valid ==0)
        {
            printf("IF cacheline index in if, i=%d, index=%d\n", i, cacheLine[indx].index);
            missNum++;
            cacheLine[indx].valid = 1;
        }
        else
        {

            if(cacheLine[indx].tag == tag)
            {
                hitNum++;
            }
            else
            {
                missNum++;
            }
            printf("ELSE cacheline index in else, i=%d, index=%d\n", i, cacheLine[indx].index);
        }
        cacheLine[indx].tag = tag;
        cacheLine[indx].dataLow = (address[i]/16)*16;
    }

    int as=0;
    for(as;as<8;as++)
    {
        printf("after loop cache[%d] index: %lx\n", as,cacheLine[as].index);
    }

    int numberOfCacheLines = 8;
    hitRate = (float)hitNum/(float)accessTime;
    missRate = (float)missNum/(float)accessTime;
    printInfo(hitNum, missNum, hitRate, missRate,numberOfCacheLines,cacheLine);
}

//the function to read address from file and store them into array
void readFile(FILE *filePtr, char* inFileName, int accessTime,
              int *address, struct Cache_file cacheLine[])
{
    //open the file
    filePtr = fopen(inFileName , "r");

    // start reading the address
    int i=0;
    for(i;i<NUMBER_OF_ADDRESS ;i++)
    {
        fscanf(filePtr, "%lX", &address[i]);
    }

    i = 0;
    for(i;i<NUMBER_OF_ADDRESS;i++)
    {
        printf("address divided by 16 mod 8: %d\n", (address[i]/BLOCK_SIZE )%8);
    }

    // default the value in every cache line
    i = 0;
    for(i;i<8;i++)
    {
        cacheLine[i].tag = 0;
        cacheLine[i].index = i;
        cacheLine[i].valid = 0;
        cacheLine[i].dataLow = 0;
        printf("initialized index: %d\n", cacheLine[i].index);
    }

    //test
    i = 0;
    for(i;i<accessTime;i++)
    {
        printf("address in array in read: %0.6lX\n", address[i]);
    }

    // pass the cache line and address array in to calculation
    checkHit(address, &cacheLine, accessTime);

    fclose(filePtr);
}


int main()
{
    // the total time to access
    // default value is Number of access;
    int accessTime = NUMBER_OF_ADDRESS;

    // declare the address array
    int address[NUMBER_OF_ADDRESS];

    //store into each structure of cache line array cacheLine[NUMBER_OF_ADDRESS]
    //  index =  (address / cache block size) % 8
    //        =  (address/ 16 bytes ) %8
    // so the range of index will be 0-7
    // declare a array of cache line index form 0-8
    struct Cache_file cacheLine[8];


    //declare the file pointer and file name
    char inFileName[] = FILE_NAME;
    FILE *ptr;

    // start the simulator by reading the file
    // put the reading address into address array
    // pass the cache line array to start the simulation
    readFile(ptr,inFileName,accessTime,&address,&cacheLine);
    return 0;
}

文本文档中的前十个地址在这里:

0x00000033
0x00000003
0x00000003
0x00000003
0x00000003
0x0000005A
0x0000000B
0x00000000
0x00000009
0x0000000A

1 个答案:

答案 0 :(得分:0)

在这一行:

checkHit(address, &cacheLine, accessTime);

应该是cacheLine,而不是&cacheLine

您的编译器应该报告此情况。例如,gcc 4.9说:

k.c:174:23: warning: passing argument 2 of 'checkHit' from incompatible pointer type
 checkHit(address, &cacheLine, accessTime);
                   ^
k.c:69:6: note: expected 'struct Cache_file *' but argument is of type 'struct Cache_file **'

如果您的编译器发出任何错误或警告(除非您100%确定它们不是错误,否则警告应被视为错误),然后在尝试运行程序之前修复这些错误。

如果编译器没有说什么,那么你真的必须弄清楚如何正确调用编译器,或使用不同的编译器。