我真的需要有人告诉我为什么这是不正确的。我不精通指针的方式来理解我所犯的错误。
template <class T> UndoArray<T>::UndoArray(uint n)
{
this->size_ = n;
this->counts_ = new uint[n]; // this tells how many objs are in || values array
values_ = new T*[n];
//declare arrays in each spot of values_
for(uint i=0; i<size_; ++i) //HERE<=========
{
values_[n] = NULL;
}
}
出于某种原因,我在此处说过,我在 Valgrind 上遇到无效写入错误。每当我写这些时,我似乎都得到了它们。有一次,该程序甚至向我抛出一个 malloc 错误,提示我试图删除尚未分配的内容。
最佳答案
values_[n] = NULL;
在这一行中,您要在分配的数组末尾之后写入一个。也许您打算使用 values_[i]
?
关于c++ - 遇到无效写入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995827/