Array of Structures Memory Issue

In my main function, I create an array of structures which stores cache information i.e. dirty bit (bool), valid bit (bool), tag (string), data (string), and age (int used for LRU policy). I then pass the array into a simulate function. Inside the function, I use a nested for loop to run through each reference from a text file in addition to updating the contents of the array of structures. However, once I leave the function, the contents of the array are erased even though I pass the array in using pointers, and another array I pass in works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
void simulate(hitormisstable& readingFile, CacheBlock *CacheMemory, std::string *hitOrMiss, int references, char replacementPolicy, int tagBits, int cmBlocks, int addressBits)
{   
    int range1 = 0; // lower limit of range
    int range2 = 0; // upper limit of range
    int max = -1; // max for LRU
    int index = 0;
    int maxIndex = 0; // index at which max occurs
    bool dirty[cmBlocks];
    bool valid[cmBlocks];
    string tag[cmBlocks];
    string data[cmBlocks];
    string binaryTag;
    

    if (replacementPolicy == 'L') // checks for LRU
    {
        for (int i = 0; i < references; i++) // iterates through number of references
        {
            range1 = getRangeOne(readingFile.cmpair, i); // collects lower limit of range
            range2 = getRangeTwo(readingFile.cmpair, i); // collects upper limit of range
            string binaryTag = toBinary(readingFile.mainMemAddress[i], tagBits, addressBits); // collects binary tag

            for (int j = range1; j <= range2; j++) // iterates through range
            {
                if (CacheMemory[j].tag == binaryTag) // checks to see if tags equal
                {
                    // std::cout << "\ntestBefore:" << hitOrMiss[i]; 
                    hitOrMiss[i] = "hit"; // if hit, then hit status is stored as a miss
                    // std::cout << "\ntestAfter:" << hitOrMiss[i];
                    
                    if (readingFile.readwrite[i] == 'W') // checks for write operation
                    {
                        dirty[j] = 1; // sets dirty bit to 1 if a write operation is detected
                        CacheMemory[j].dirtybit = dirty;
                    }

                    for (int k = range1; k <= range2; k++) // iterates through range
                    {
                        CacheMemory[k].age++; // increments age of all data sets
                    }

                    CacheMemory[j].age = 0; // sets age of least recently used block to 0
                    break; // breaks out of loop
                }
                hitOrMiss[i] = "miss"; // hit status is stored as miss if hit does not occur
                
            }

            

            max = -1; // resets max
            maxIndex = -1; // resets maxindex

            for (int j = range1; j <= range2; j++) // itertes through range
            {
                if (max < CacheMemory[j].age) // checks for max age
                {
                    max = CacheMemory[j].age; // max is set to max age
                    maxIndex = j; // j is stored as max indexc
                }
            }

            dirty[maxIndex] = 0;
            CacheMemory[maxIndex].validbit = 1; // sets valid bit to 1
                if (readingFile.readwrite[i] == 'W') // checks for write operation
                {
                    dirty[maxIndex] = 1; // sets dirty bit to 1 if write operation is detected
                }
                else
                {
                    CacheMemory[maxIndex].dirtybit = 0; // sets dirty bit to 0 if read operation
                }
                tag[maxIndex] = binaryTag; // sets cache memory tag to binary tag
                //cout << "In for loop: " << tag[maxIndex] << endl;
                data[maxIndex] = convertIntToString(readingFile.mainMemBlock[i]); // sets cache memory data to the the mm blk # of reference
                for (int k = range1; k <= range2; k++) // iterates through age
                {
                    CacheMemory[k].age++; // increments age of each data member
                }
                CacheMemory[maxIndex].age = 0; // sets age of LRU block to 0
                CacheMemory[maxIndex].dirtybit = dirty[maxIndex];
                CacheMemory[maxIndex].tag = tag[maxIndex];
                CacheMemory[maxIndex].data = data[maxIndex];

                //cout << "In for loop: " << CacheMemory[i].tag[i] << endl;
        }
        //cout << "After for loop: " << CacheMemory[3].tag << endl;
    }
    if (replacementPolicy == 'F') // checks for FIFO replacement policy
    {
        int fifoCounter = 0; // counter to determine where to replace
        int fifoSum = 0; // sum of fifoCounter and range1

        //FIFO Replacement
        for (int i = 0; i < references; i++) // cycles through all references from input file
        {
            range1 = getRangeOne(readingFile.cmpair, i); // collects the first part of the range
            range2 = getRangeTwo(readingFile.cmpair, i); // collects the second part of thr range
            std::string binaryTag = toBinary(readingFile.mainMemAddress[i], tagBits, addressBits); // gets the tag of the binary address

            // checks along the range
            for (int j = range1; j <= range2; j++)
            {
                if (CacheMemory[j].tag == binaryTag) // checks to see if tags match
                {
                    hitOrMiss[i] = "hit"; // puts the string hit into hitStatus if there is a hit

                    if (readingFile.readwrite[i] == 'W') // checks for write operation
                    {
                        dirty[j] = 1; // sets dirty bit to 1 if write operation detected
                        CacheMemory[j].dirtybit = 1;
                    }
                    break; // breaks out of loop
                }
                hitOrMiss[i] = "miss"; // miss if hit is not detected
                
            }

            fifoSum = fifoCounter + range1; // gets the sum of the fifo counter and range1

            if (fifoSum > range2 - 1) // checks to see if the sum is inbounds
            {
                fifoCounter = 0; // if it's out of bounds, it resets the counter
            }

            CacheMemory[fifoSum].validbit = 1; // sets the valid bit to 1
            valid[fifoSum] = 1;
            if (readingFile.readwrite[i] == 'W') // checks for write operation
            {
                CacheMemory[fifoSum].dirtybit = 1; // if write operation is detected, it sets the dirty bit to 1
                dirty[fifoSum] = 1;
            }
            else
            {
                CacheMemory[fifoSum].dirtybit = 0; // sets dirty bit to 0 if read operation
                dirty[fifoSum] = 0;
            }
            CacheMemory[fifoSum].tag = binaryTag; // sets the tag of the cache memory to the calculate tag
            tag[fifoSum] = binaryTag;
            CacheMemory[fifoSum].data = convertIntToString(readingFile.mainMemBlock[i]); // sets the data of the cache memory
            data[fifoSum] = convertIntToString(readingFile.mainMemBlock[i]);
            fifoCounter++; // increments fifocounter
        }
    }
}

Array used: CacheBlock CacheMemory[numberofCacheBlocks]; 
Last edited on
@brharding1,
PLEASE USE CODE TAGS (the <> formatting button to the right), when posting code.

Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.

Tutorials on how to use code tags:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

I found the second link to be the most help.

Hint: You can hit "edit post", highlight your code and then press the <> formatting button. This will not automatically indent your code. That part is up to you.

I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted.

You can use the "preview" button at the bottom to see how it looks.

Thanks!
max
It would help to see the code that's calling the function.

How certain are you that the function is doing what you intend it to do? Have you stepped through it with your debugger, to check that the array is actually being updated the way you think it is?

Have you used your debugger to examine the contents of the array in the calling code, to be certain that it's not being passed back correctly?


Topic archived. No new replies allowed.