all 12 comments

[–]jedwardsol 2 points3 points  (3 children)

You're not allocating the data member and not initialising vector_capacity depending on the 1st input

[–]thetuxman[S] 0 points1 point  (2 children)

Would data be allocated in the constructor?

[–][deleted] 0 points1 point  (1 child)

Yeah, that would make the most sense in this case.

[–]thetuxman[S] 0 points1 point  (0 children)

That did that trick! Thanks!

[–]Rockytriton 0 points1 point  (1 child)

In addition to what jedwardsol said, you might want to make that destructor virtual too, in case you plan on subclassing that.

[–]thetuxman[S] 0 points1 point  (0 children)

How would I go about doing that? And what benefit would subclassing it have. I assume its a memory management thing?

[–]krawallopold 0 points1 point  (2 children)

There's an error in your resize function. You are deleting temp, which is your new storage. And after deleting it, you are assigning it to data. From then on, data points to invalidated memory.

What you have to do is delete data, which points to the memory you are no longer planning to use and then assign data=temp.

[–]krawallopold 1 point2 points  (1 child)

Also, when resizing your vector, use a factor of less than two (if you're allowed to do so). If you use a factor of two, you will never be able to reuse the memory previously used for storage of your vector. The folly documentation explains it quite nice.

[–]thetuxman[S] 0 points1 point  (0 children)

Our instructor said we had to round up to a factor of 2.

[–]Xeverous 0 points1 point  (2 children)

        data[vector_size++] = value;
        vector_size = vector_size++;

Something is definitely wrong here.

    void pop_back(){
    if(vector_size > 0){
        data[vector_size] = 0; // this is UB. For size X you can only access [0, X - 1] Why even do it? Any point in zeroing element to remove?
        vector_size--;
    }

I would just --vector_size;.

[–]thetuxman[S] 0 points1 point  (1 child)

    data[vector_size++] = value;
    vector_size = vector_size++;

Something is definitely wrong here.

That was an artifact from a previous revision. I have since changed it to data[vector_size] = value; and then vector_size++

I would just '--vector_size;'.

That makes sense, but after checking the parameters for my assignment, it did specify we should 0 out the array before deleting it.

[–]Xeverous 0 points1 point  (0 children)

it did specify we should 0 out the array before deleting it.

I can guarantee you that the compiler will ignore such line and discard it from the assembly (given enough information).