-1

I am facing an issue of trying to get the initial capacity to match with the correct one. I have been trying to get the starting capacity to start at 0 instead of 1 (as shown below). However, I can't seem to do it. I would really appreciate if you can tell me where I've gone wrong with my codes. Thank you in advance!

Correct output:

********** TestPush **********
Empty array:
(size=0, capacity=0)
push_back 5 floats:
0  (size=1, capacity=1)
0      1  (size=2, capacity=2)
0      1      2  (size=3, capacity=4)
0      1      2      3  (size=4, capacity=4)
0      1      2      3      4  (size=5, capacity=8)
pop_back until empty:
0      1      2      3  (size=4, capacity=8)
0      1      2  (size=3, capacity=8)
0      1  (size=2, capacity=8)
0  (size=1, capacity=8)
(size=0, capacity=8)

Incorrect output:

********** TestPush **********
Empty array:
(size=0, capacity=1)
push_back 5 floats:
0  (size=1, capacity=1)
0      1  (size=2, capacity=2)
0      1      2  (size=3, capacity=4)
0      1      2      3  (size=4, capacity=4)
0      1      2      3      4  (size=5, capacity=8)
pop_back until empty:
0      1      2      3  (size=4, capacity=8)
0      1      2  (size=3, capacity=8)
0      1  (size=2, capacity=8)
0  (size=1, capacity=8)
(size=0, capacity=8)

My .h file

template <typename T>
class vector 
{

private:
 T* v;
 int count;
 int capacity;
public:

vector(void){

    v = new T[capacity];
    count = 0;
    capacity=0;
    capacity++;
}
void push_back(const T& t)
{
    if(count+1>capacity)
{
    capacity *= 2;
    T* newData = new T[capacity];
    for(int i=0; i <count; i++)
    {
        newData[i] = v[i];
    }
    delete[] v;
    v = newData;
}

v[count++] = t;
}

main file:

void TestPush(void)
{
std::cout << "\n********** TestPush **********\n";
cs150::vector<float> a;
std::cout << "Empty array:\n";
Print(a);

std::cout << "push_back 5 floats:\n";
for (float i = 0; i < 5; i++) {
    a.push_back(i);
    Print(a);
}

std::cout << "pop_back until empty:\n";
while (!a.empty()) {
    a.pop_back();
    Print(a);
}
}
TrebledJ
  • 8,713
  • 7
  • 26
  • 48

2 Answers2

2

The problem is that you are using capacity in the line

v = new T[capacity];

before capacity has been initialized. That is cause for undefined behavior.

You should initialize capacity to a value greater than 0 first.

vector(void) : count(0), capacity(1) 
{
    v = new T[capacity];
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Well it seems fairly clear, this is your code

vector(void){

    v = new T[capacity];
    count = 0;
    capacity=0;
    capacity++;
}

Don't increment the capacity, do this instead

vector() {    
    count = 0;
    capacity = 0;
    v = new T[capacity];
}

Note that you must set the capacity variable before you do new T[capacity]; void is unecessary BTW

Now since your capacity is one less than previously you need some other changes

void push_back(const T& t)
{
    if(count+1>capacity)
    {
        capacity *= 2;

becomes

void push_back(const T& t)
{
    if (count+1>capacity)
    {
        capacity = std::max(2*capacity, 1);

So that if the capacity is zero before it will be one afterwards

john
  • 85,011
  • 4
  • 57
  • 81
  • I would use `if (count==capacity)` instead of `if (count+1>capacity)`. And if you are going to introduce STL algorithms, then you may as well replace the manual copy loop with `std::copy()` instead – Remy Lebeau Mar 01 '19 at 21:07