[英]Dynamic array resize function problems
NOTE: I am aware that it would be easier to just use the STL Vector, however, for a programming class that I'm in, we are required to write our own dynamic array template class because our professor apparently enjoys making us reinvent the wheel.注意:我知道只使用 STL Vector 会更容易,但是,对于我所在的编程课程,我们需要编写自己的动态数组模板类,因为我们的教授显然喜欢让我们重新发明轮子.
Anyway, I made a resize function for my dynamic array template class that goes as follows.无论如何,我为我的动态数组模板类做了一个调整大小的函数,如下所示。 Note that the private member variables are T* arr, unsigned used, and unsigned cap.请注意,私有成员变量是 T* arr、unsigned used 和 unsigned cap。
template <class T>
void darray<T>::resize(unsigned size)
{
if (size > cap)
{
T* temp_arr = new T[size];
for (int count = 0; count < used; ++count)
temp_arr[count] = arr[count];
for (int count = used; count < size; ++count)
temp_arr[count] = T();
delete []arr;
arr = temp_arr;
cap = size;
used = size;
}
if (size < cap)
{
used = size;
}
}
Whenever I use this function to increase the size of the array, it will work the first time I need to use it, but after that, Visual Studios will trigger a breakpoint at line 14 ( delete[] arr
), and if I continue past the breaks, I eventually get _CrtIsValidHeapPointer(pUserData)
assertion failure.每当我使用这个函数来增加数组的大小时,它会在我第一次需要使用它时起作用,但是在那之后,Visual Studios 将在第 14 行( delete[] arr
)触发一个断点,如果我继续过去中断,我最终得到_CrtIsValidHeapPointer(pUserData)
断言失败。 What is causing this, and how can I fix it?这是什么原因造成的,我该如何解决?
constructor:构造函数:
template <class T>
darray<T>::darray()
{
used = 0;
cap = 64;
arr = new T[cap];
for (int count = 0; count < cap; ++count)
arr[count] = T();
} }
assignment operator:赋值运算符:
template <class T>
darray<T>& darray<T>::operator= (const darray& right_operand)
{
delete[] arr;
arr = new T[right_operand.capacity()];
used = right_operand.size();
cap = right_operand.capacity();
for (int count = 0; count < used; ++count)
arr[count] = right_operand[count];
return *this;
}
destructor:析构函数:
template <class T>
darray<T>::~darray()
{
delete[] arr;
}
There's been some requests for the push_back function, so here's that as well:有一些对 push_back 函数的请求,所以这里也是:
template <class T>
void darray<T>::push_back(const T& input)
{
if ((used + 1) > cap)
{
resize(cap * 2);
arr[used + 1] = input;
++used;
}
else
{
arr[used] = input;
++used;
}
}
Your resize
function increases used
.你的resize
函数增加了used
。 When you access arr[used+1]
in push_back
, than you access an invalid array location.当您在push_back
访问arr[used+1]
时,您访问的是无效的数组位置。
You should add another function, which is similar to resize
, but changes only the capacity of your array and not the stored object count.您应该添加另一个类似于resize
函数,但仅更改数组的容量,而不更改存储的对象计数。 (ie it does not increment used
). (即它不会增加used
)。 This function should be called by push_back
.这个函数应该由push_back
调用。 (As you mentioned std::vector
in your question, see the difference between vector::resize and vector::reserve .) (正如您在问题中提到的std::vector
,请参阅vector::resize和vector::reserve之间的区别。)
Also: array indexes are zero-based.另外:数组索引是从零开始的。 Do not insert at position used + 1
but on position used
.不要在used + 1
位置used + 1
处插入,而是在used
位置插入。
I would use next implementation.我会使用下一个实现。 I assume that "cap" member is for capacity of vector and "used" is for amount of elements in array.我假设“cap”成员是向量的容量,而“used”是数组中元素的数量。
template <class T>
void darray<T>::resize(unsigned size)
{
if (size > cap)
{
cap = size * 2;
T* temp_arr = new T[cap];
for (int count = 0; count < used; ++count)
temp_arr[count] = arr[count];
delete [] arr;
arr = temp_arr;
}
// zero members if size was decreased
if (size < used)
{
for (int count = size; count < used; ++count)
arr[count] = T();
}
used = size;
}
Pros: You don't need to reallocate the whole array each time you do a resize with larger number of elements.优点:每次使用大量元素调整大小时,您都不需要重新分配整个数组。
Cons: You spend an extra space when you don't need to.缺点:当您不需要时,您会花费额外的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.