为什么类中的向量自动变为 NULL
why vector in class automatically become NULL
我正在创建简单的缓存模型来模拟某些应用程序。
但是,在下面的代码lru
中发生了问题。(我没有复制不相关的代码)
主.cpp
int main(void) {
Cache* L1Cache = new Cache(64, 64, 8);
Cache* L2Cache = new Cache(256, 64, 4);
Cache* L3Cache = new Cache(2048, 64, 16); // This object causes problem
Cache* L4Cache = new Cache(2048, 64, 8);
L1Cache->initCache();
L2Cache->initCache();
L3Cache->initCache();
L4Cache->initCache();
return 0;
}
缓存.h
typedef struct CacheLine {
std::vector<uint64_t> data;
}CacheLine;
typedef struct CacheSet {
std::vector<bool> valid;
std::vector<uint64_t> tag;
std::vector<CacheLine> directory;
}CacheSet;
typedef struct LRU {
std::vector<std::vector<bool>> lruMatrix;
}LRU;
class Cache {
public:
Cache(uint32_t cacheSizeInKB, uint32_t lineSizeInByte, uint32_t numOfDirs) {
// set cache size
this->cacheSizeInKB = cacheSizeInKB;
this->lineSizeInByte = lineSizeInByte;
this->numOfDirs = numOfDirs;
this->numOfSets = (cacheSizeInKB * 1024) / (lineSizeInByte * numOfDirs);
// set memory address offset
this->blockOffsetFrom = log2(lineSizeInByte) - 1;
this->blockOffsetTo = 0;
this->indexOffsetFrom = this->blockOffsetFrom + ceil(log2(this->numOfSets));
this->indexOffsetTo = this->blockOffsetFrom + 1;
this->tagOffsetFrom = 63;
this->tagOffsetTo = this->indexOffsetFrom + 1;
// reserve vectors before using
cache.reserve(this->numOfSets);
for (int x = 0; x < this->numOfSets; ++x) {
cache[x].valid.reserve(numOfDirs);
cache[x].tag.reserve(numOfDirs);
cache[x].directory.reserve(numOfDirs);
for (int y = 0; y < this->numOfDirs; ++ y) {
cache[x].directory[y].data.reserve(lineSizeInByte / 8);
}
}
lru.reserve(this->numOfSets);
for (int i = 0; i < this->numOfSets; ++i) {
lru[i].lruMatrix.reserve(numOfDirs);
for (int j = 0; j < this->numOfDirs; ++j) {
lru[i].lruMatrix[j].reserve(numOfDirs);
}
}
std::cout << "1: " << &lru[0].lruMatrix[0] << std::endl; // this shows correct memory address space
}
void initCache();
void accessData(uint64_t addr);
void printLRUMatrix(uint64_t index);
private:
const uint32_t HIT = 1;
const uint32_t MISS = 0;
// cache size list
uint32_t cacheSizeInKB;
uint32_t lineSizeInByte;
uint32_t numOfDirs;
uint32_t numOfSets;
// offset list
uint64_t blockOffsetFrom;
uint64_t blockOffsetTo;
uint64_t indexOffsetFrom;
uint64_t indexOffsetTo;
uint64_t tagOffsetFrom;
uint64_t tagOffsetTo;
std::vector<CacheSet> cache;
std::vector<LRU> lru;
};
缓存.cpp
void Cache::initCache() {
for (int x = 0; x < numOfSets; ++x) {
for (int i = 0; i < numOfDirs; ++i) {
cache[x].valid[i] = false;
cache[x].tag[i] = 0;
for (int j = 0; j < numOfDirs; ++j)
cache[x].directory[i].data[j] = 0;
}
}
std::cout <<"2: " << &lru[0].lruMatrix[0] << std::endl; // This prints out 0 address in case of L3Cache
/*
for (int i = 0; i < numOfSets; ++i) {
std::cout << "i: " << i << std::endl;
for (int j = 0; j < numOfDirs; ++j) {
std::cout << "j: " << j << std::endl;
for (int k = 0; k < numOfDirs; ++k) {
std::cout << "k: " << k << std::endl;
this->lru[i].lruMatrix[j][k] = false;
}
}
}
*/
}
输出
1: 0x9464d0
1: 0x9f5190
1: 0xded230
1: 0x140d2d0
2: 0x9464d0
2: 0x9f5190
2: 0
2: 0x140d2d0
我从上面的代码中遇到了奇怪的情况。
在 L3Cache 的情况下,lru[0].lruMatrix[0] 的地址在缓存constructor
(0xded230) 和成员函数initCache()
(0) 之间是不同的。
但是,其他情况(例如L1Cache,L2Cache,L4Cache)在constructor
和initCache()
之间打印正确(相同)地址。
唯一的区别是L3Cache使用比其他的更大的numOfDir 16。
我不知道为什么会这样。我的代码似乎没有错误。
有什么问题吗?
你正在越界阅读lru
。
假设在运行Cache::initCache()
之前没有其他内容触及Cache::lru
,则默认情况下,此std::vector
将以空状态初始化自身。由于该函数中的任何内容都不会增加lru
的大小,因此当您点击此行时,它仍然是空的:
std::cout <<"2: " << &lru[0].lruMatrix[0] << std::endl;
在其中,你有lru[0]
.这是取消引用lru
的第一个元素,它不存在。这是未定义的行为。任何事情都可能发生。别这样。在取消引用之前,您需要确保位置0
处存在某些内容。
lru[0].lruMatrix
处的向量地址显示为零的原因可能是因为vector
最初将其动态分配的指向数组的指针设置为空指针。因此,取消引用第一个元素就是取消引用空指针。这是您的特定标准库供应商的实现细节;不要依赖此行为。
我还在您的代码中看到以下模式:
lru.reserve(this->numOfSets);
for (int i = 0; i < this->numOfSets; ++i) {
lru[i].doSomething();
...
出于同样的原因,这是未定义的行为。std::vector::reserve
不会更改矢量的大小。它只分配存储。你可能想要std::vector::resize
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- 我可以为向量函数返回 NULL<double> 吗?
- 使用 push_back() 时指针指向 NULL 的向量
- 为什么类中的向量自动变为 NULL
- 对象向量中的空对象与指针向量中的NULL/nullptr
- 在std向量中查找NULL指针
- basic_string::_M_construct null 在构造字符串的子向量后无效
- 如何用NULL(c++)初始化对的向量
- 指针指向NULL向量
- 不能将NULL发送给获取向量的函数
- 在一个向量中找到NULL
- 与向量一起使用的C++可为Null/可选项的整数
- 检查在模板第一个参数之后声明的函数是否为 NULL(传递向量和数组)