0

Hi i was creating a simple Vector Class for my studies. Just to store Variable LONG i kept everything simple. When i build nothing was return error/ warning. Then after run the program, it works but the program crash.

Code Vector.h

class Vector{
public:
    Vector();
    void add(long i);
    void getData();
private:
    long *anArray;
    long maxSize;
    long position;
    void reSize(int i);

};

Vector.cpp

    Vector::Vector(){
    maxSize = 2;
    anArray = new long[maxSize];
    position = 0;
}

void Vector::add(long i){
    if(position==maxSize-1)
        reSize(maxSize * 2);

    anArray[position] = i;
    position++;
}

void Vector::reSize(int i){
    long *temp = new long[maxSize];
    for(int i =0; i<maxSize; i++)
    {
        temp[i] = anArray[i];
    }
    delete[] anArray;
    anArray = temp;
}

void Vector::getData(){

    for(int i = 0; i<position; i++)
    {
        cout << "Element" << i+1 << " : " << anArray[i] << endl;
    }
}

Main

    int main()
{
    Vector vecStore;



    for(int i = 0; i <  1000; i++)
    {
        long a;
        vecStore.add(a = i + 1);
    }

     cout << "GET DATA _________ :: " << endl;
    vecStore.getData();
    return 0;
}

The program wouldn't crash if the input data small(e.g. 10-20) but when i change it to 100 or even bigger. the program sometime crash and sometime its not.

Did i make a mistake?

Fernando Ciam
  • 21
  • 2
  • 6

1 Answers1

1

In

void Vector::add(long i){
    if(position==maxSize-1)
        reSize(maxSize * 2);

    anArray[position] = i;
    position++;
}

when position==maxSize-1, reSize is called to double the size of the array. Unfortunately, reSize is broken.

void Vector::reSize(int i){
    long *temp = new long[maxSize];
    for(int i =0; i<maxSize; i++)
    {
        temp[i] = anArray[i];
    }
    delete[] anArray;
    anArray = temp;
}

The parameter i is never used. This should be the new size of the Vector, but instead the new array is allocated with the same maxSize as the previous array. maxSize goes unchanged.

Back in add, data is stored at position and position is increased by one. This means on the next add position is equal to maxSize, so the check to prevent overrunning the array,

if(position==maxSize-1)

will not do what you want and the program writes outside the bounds of the array. From there, position keeps getting larger, will never be equal to maxSize minus one, and writes even further out of bounds, not that it matters at that point. Undefined Behaviour has already been invoked, and Crom only knows what will happen. Maybe the program crashes, maybe the program crashes later, maybe it does something bizarre, and maybe it even chugs on looking like everything is working just fine.

You want something more along the lines of

void Vector::reSize(int newSize){ // note more descriptive name
    long *temp = new long[newSize]; // using new size, not old size
    for(int i =0; i<maxSize; i++)
    {
        temp[i] = anArray[i];
    }
    delete[] anArray;
    anArray = temp;
    maxSize = newSize; // store new size.
}

Vector also needs a destructor to clean up anArray, but in order to properly fix this error, here's a bit of recommended reading: What is The Rule of Three?

user4581301
  • 33,082
  • 7
  • 33
  • 54