Array of structs printing and modifying

-1

So I have a homework that is using c program to read the address from text document and to check if it's hit or miss When I do the final process to print the index and the value, the garbage number occurs, but I don't know why it always print out something like this:

printinfo index loop: 8
printinfo index loop: 779318117

Which is definitely wrong since the index should be 0-7, not 8 or this funky number

Here is the code:

    #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;
};


//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)
{


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;
}

The first ten address in text document is here:

0x00000033
0x00000003
0x00000003
0x00000003
0x00000003
0x0000005A
0x0000000B
0x00000000
0x00000009
0x0000000A
c
file
asked on Stack Overflow Nov 25, 2015 by WhiteJade • edited Nov 26, 2019 by WhiteJade

1 Answer

0

In this line:

checkHit(address, &cacheLine, accessTime);

it should be cacheLine, not &cacheLine.

Your compiler should have reported this. For example, gcc 4.9 says:

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 **'

If your compiler gives any errors or warnings (warnings should be treated by you as errors unless you are 100% sure they are not errors) , then fix those things before trying to run your program.

If your compiler didn't say anything then you really must figure out how to invoke the compiler properly, or use a different compiler.

answered on Stack Overflow Nov 26, 2015 by M.M • edited Nov 26, 2015 by M.M

User contributions licensed under CC BY-SA 3.0