push_back()
and insert()
both call grow()
, which fails to increase the capacity
because GROWER
is an int
, so 1.6
truncates to 1
, and multiplying capacity * 1
doesn’t change its value. But even if capacity
were increased properly, the data_ptr
array is not being re-allocated at all to fit the new capacity
.
But even if grow()
were working properly, there is no need to call grow()
on every insertion of a new element, that defeats the purpose of separating n_elems
from capacity
to begin with. You should grow the array only when n_elems
has reached capacity
.
There are many other problems with your class, as well:
-
operator=
is not testing for self-assignment, and is leaking the allocated memory of the old array. Consider using the copy-swap idiom instead. -
front()
does not reach thethrow
statement when the array is empty. -
at()
anderase()
are performing bounds checking usingcapacity
instead ofn_elems
. -
push_back()
is inserting the new value at the wrong index, and not incrementingn_elems
. -
pop_back()
does notthrow
an error when the array is empty, causingn_elems
to decrement below0
, which wraps around to the max positive value ofsize_t
becausesize_t
is unsigned. -
erase()
andoperator==
go out of bounds while iterating the array. -
begin()
andend()
should not be returningnullptr
for an empty array. Andend()
is returning a pointer to the last element in a non-empty array rather than returning a pointer to 1 past the last element. -
operator==
andoperator!=
do not perform any bounds checking to make sure the 2 vectors have the same samen_elems
before iterating their arrays. And they are comparing element values beyondn_elems
. Also,operator!=
is returning the wrong result.
With that said, try something more like this:
#include <stdexcept>
#include <algorithm>
#include "Vector.h"
void Vector::grow()
{
static const float GROWER = 1.6f;
size_t new_capacity = capacity * GROWER;
if (new_capacity <= capacity)
throw std::overflow_error("cant grow capacity");
int *new_data_ptr = new int[new_capacity];
std::copy(data_ptr, data_ptr + n_elems, new_data_ptr);
delete[] data_ptr;
data_ptr = new_data_ptr;
capacity = new_capacity;
}
Vector::Vector()
{
capacity = CHUNK;
n_elems = 0;
data_ptr = new int[capacity];
}
Vector::Vector(const Vector& v)
{
capacity = v.capacity;
n_elems = v.n_elems;
data_ptr = new int[capacity];
std::copy(v.begin(), v.end(), data_ptr);
}
Vector& Vector::operator=(const Vector& v)
{
if (this != &v)
{
Vector temp(v);
std::swap(capacity, temp.capacity);
std::swap(n_elems, temp.n_elems);
std::swap(data_ptr, temp.data_ptr);
}
return *this;
}
Vector::~Vector()
{
delete[] data_ptr;
}
int Vector::front() const
{
if (n_elems == 0)
throw std::range_error("Range Error");
return data_ptr[0];
}
int Vector::back() const
{
if (n_elems == 0)
throw std::range_error("Range Error");
return data_ptr[n_elems - 1];
}
int Vector::at(size_t pos) const
{
if (pos >= n_elems)
throw std::range_error("Range Error");
return data_ptr[pos];
}
size_t Vector::size() const
{
return n_elems;
}
bool Vector::empty() const
{
return (n_elems == 0);
}
int& Vector::operator[](size_t pos)
{
return data_ptr[pos];
}
void Vector::push_back(int item)
{
if (n_elems == capacity)
grow();
data_ptr[n_elems] = item;
++n_elems;
}
void Vector::pop_back()
{
if (n_elems == 0)
throw std::range_error("Range Error");
--n_elems;
}
void Vector::erase(size_t pos)
{
if (pos >= n_elems)
throw std::range_error("Range Error");
std::copy(data_ptr + pos + 1, data_ptr + n_elems, data_ptr + pos);
--n_elems;
}
void Vector::insert(size_t pos, int item)
{
if (pos > n_elems) // insert at end() is OK...
throw range_error("Range Error");
if (n_elems == capacity)
grow();
std::copy_backward(data_ptr + pos, data_ptr + n_elems, data_ptr + n_elems + 1);
data_ptr[pos] = item;
++n_elems;
}
void Vector::clear()
{
n_elems = 0;
}
int* Vector::begin()
{
return data_ptr;
}
int* Vector::end()
{
return data_ptr + n_elems;
}
bool Vector::operator==(const Vector& v) const
{
return (n_elems == v.n_elems) && std::equal(v.begin(), v.end(), data_ptr);
}
bool Vector::operator!=(const Vector& v) const
{
return !(*this == v);
}
1
solved Errors while creating a dynamic array in a vector class for project [closed]