Unwanted Variable Change in While Loop

Hey guys, I'm struggling hard with this one. I'm working on a cache simulator for one of my classes and I'm having an issue with variables changing by themselves, but in a peculiar way. For example, if I initialize blockSize to 16, it will be 16 for the first time through the while loop only. But then it will drop down to 10 and stay there for the duration of the program.

My initial thought was that the addresses of the changing variables were being highjacked somewhere, but I couldn't find any instances.

I'm somewhat new to C++, so if anyone could at least point me in the right direction, I would greatly appreciate it. Thanks!

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <fstream>
#include <cmath>
#include <math.h>
#include <tgmath.h>
#include <sstream>
#include <stdlib.h>
#include <string>
#include "BaseCache.h"
using namespace std;

BaseCache::BaseCache(uint32_t _cacheSize, uint32_t _associativity, uint32_t _blockSize) {
   setCacheSize(_cacheSize);
   setAssociativity(_associativity);
   setBlockSize(_blockSize);
   numSets = cacheSize/(blockSize*associativity);
   createCache();
   resetStats();
}

void BaseCache::setCacheSize(uint32_t _cacheSize) {
    cacheSize = _cacheSize;
}
void BaseCache::setAssociativity(uint32_t _associativity) {
    associativity = _associativity;
}
void BaseCache::setBlockSize(uint32_t _blockSize) {
    blockSize = _blockSize;
}

uint32_t BaseCache::getCacheSize() {
    return cacheSize;}
uint32_t BaseCache::getAssociativity() {
    return associativity;}
uint32_t BaseCache::getBlockSize() {
    return blockSize;}

uint32_t BaseCache::getReadHits() {
    return numReadHits;}
uint32_t BaseCache::getReadMisses() {
    return numReadMisses;}
uint32_t BaseCache::getWriteHits() {
    return numWriteHits;}
uint32_t BaseCache::getWriteMisses() {
    return numWriteMisses;}
double BaseCache::getReadHitRate() {
    double numReadMisses = getReadMisses();
    double numReadHits = getReadHits();
    double ReadHitRate = numReadHits/(numReadHits+numReadMisses)*100;
    return ReadHitRate;}
double BaseCache::getReadMissRate() {
    double numReadMisses = getReadMisses();
    double numReadHits = getReadHits();
    double ReadMissRate = numReadMisses/(numReadHits+numReadMisses)*100;
    return ReadMissRate;}
double BaseCache::getWriteHitRate() {
    double numWriteMisses = getWriteMisses();
    double numWriteHits = getWriteHits();
    double WriteHitRate = numWriteHits/(numWriteHits+numWriteMisses)*100;
    return WriteHitRate;}
double BaseCache::getWriteMissRate() {
    double numWriteMisses = getWriteMisses();
    double numWriteHits = getWriteHits();
    double WriteMissRate = numWriteMisses/(numWriteHits+numWriteMisses)*100;
    return WriteMissRate;}
double BaseCache::getOverallHitRate() {
    double numReadMisses = getReadMisses();
    double numReadHits = getReadHits();
    double numWriteMisses = getWriteMisses();
    double numWriteHits = getWriteHits();
    double OverallHitRate = (numReadHits+numWriteHits)/(numReadMisses+numReadHits+numWriteMisses+numWriteHits)*100;
    return OverallHitRate;}
double BaseCache::getOverallMissRate() {
    double numReadMisses = getReadMisses();
    double numReadHits = getReadHits();
    double numWriteMisses = getWriteMisses();
    double numWriteHits = getWriteHits();
    double OverallMissRate = (numReadMisses+numWriteMisses)/(numReadMisses+numReadHits+numWriteMisses+numWriteHits)*100;
    return OverallMissRate;}

void BaseCache::initDerivedParams() {
    numSets = cacheSize/(blockSize*associativity);
}

void BaseCache::resetStats() {
    BaseCache::numReads = 0;
	BaseCache::numWrites = 0;
	BaseCache::numReadHits = 0;
	BaseCache::numReadMisses = 0;
	BaseCache::numWriteHits = 0;
	BaseCache::numWriteMisses = 0;
}

//WRITE ME
//Create cache and clear it
void BaseCache::createCache()
{
    associativity = getAssociativity();
    blockSize = getBlockSize();
    cacheSize = getCacheSize();
    numSets = cacheSize/(blockSize*associativity);

    cacheLines = new cacheLine*[associativity*blockSize];

    for(uint32_t i = 0; i < numSets; i++)
    {
        cacheLines[i] = new cacheLine[numSets];
    }

    uint32_t zero = 0;
    uint32_t* p = &zero;

   for(uint32_t i = 0; i < numSets; i++)
    {
        for(uint32_t j = 0; j < numSets; j++)
        {
            cacheLines[i][j].data = p;
            cacheLines[i][j].LRUStackBits = 0;
            cacheLines[i][j].tag = 0;
        }
    }
}


bool BaseCache::read(uint32_t addr, uint32_t *data) {
    numReads++;

    uint32_t byteInt = addr % blockSize;
    uint32_t blockInt = ((addr-byteInt)/blockSize) % associativity;
    uint32_t setInt = ((addr-byteInt-blockInt)/(blockSize*associativity)) % numSets;
    uint32_t tagInt = ((addr-byteInt-blockInt-setInt)/cacheSize);

    numReadHits++;
    return 1;
}

bool BaseCache::write(uint32_t addr, uint32_t data) {
   numWrites++;

    uint32_t byteInt = addr % blockSize;
    uint32_t blockInt = ((addr-byteInt)/blockSize) % associativity;
    uint32_t setInt = ((addr-byteInt-blockInt)/(blockSize*associativity)) % numSets;
    uint32_t tagInt = ((addr-byteInt-blockInt-setInt)/cacheSize);

    if(tagInt != cacheLines[setInt][blockInt*blockSize+byteInt].tag){
        numWriteMisses++;
        uint32_t start_location = blockInt*blockSize-byteInt;
        for(uint32_t i = 0; i < 4; i++){
            cacheLines[setInt][blockInt*blockSize+byteInt].data == data;
        }
        cout << start_location << endl;
        return 0;
    }

    numWriteHits++;
    return 1;
}

BaseCache::~BaseCache() {
    delete cacheLines;
}

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "BaseCache.h"
using namespace std;

int main(int argc, char **argv)
{
    string inp_line, parsed_line;
    string command;
    unsigned int address, data;
    uint32_t cacheSize, associativity, blockSize;
    cacheSize = atoi(argv[1]);
    associativity = atoi(argv[2]);
    blockSize = atoi(argv[3]);
    ifstream fp_inp(argv[4]);

    BaseCache BaseCache(cacheSize, associativity, blockSize);

    if(fp_inp.is_open()) {
        while(getline(fp_inp, inp_line)) {
            stringstream inp_string(inp_line);
            cout << cacheSize << endl;
            if(getline(inp_string,parsed_line, ' '))
                command = parsed_line;

            if(getline(inp_string,parsed_line, ' '))
                address = stoul(parsed_line, NULL, 16);

            if (!(command.compare("w"))) {
                if(getline(inp_string,parsed_line, ' ')) {
                    data = stoul(parsed_line, NULL, 16);
                }
            }

            // Issue read/write command as per the input trace.

            if (!(command.compare("w"))) {
                if(BaseCache.write(address, data)) {
                    cout <<"Write Hit : addr:0x"<<hex<<address;
                    cout <<"      data:  0x"<<hex<<data<<"\n";
                }
                else {
                    cout <<"Write miss: addr:0x"<<hex<<address;
                    cout <<"      data: 0x"<<hex<<data<<"\n";
                }
            }

            if (!(command.compare("r"))) {
                if(BaseCache.read(address, &data)) {
                    cout <<"Read Hit  : addr:0x"<<hex<<address;
                    cout <<"      data: 0x"<<hex<<data<<endl;
                }
                else {
                    cout <<"Read miss : addr:0x"<<hex<<address;
                    cout <<"      data: Unavailable"<<endl;
                }
            }
        }

        fp_inp.close();
    }
    return 1;
}
closed account (48T7M4Gy)
I'm not going to wade through but my guess is it has something to do with 16 decimal being 10 hex more than unexplained or magical value changes and or, especially, hijackings of any kind.

You need to debug/trace variable values. Use breakpoints etc.
SOLVED:

Ahhh, that was absolutely it. I was having a similar "problem" with 512 changing to 200 (which is just hex for 512). Thank you!
Topic archived. No new replies allowed.