create my own basic vector class using realloc

First off thanks for any help I can get here, I'm pulling my hair out in frustration with my lack of knowledge on this subject.

My professor has asked us to write a basic vector class, I had written one however it was using a set size T element[100] and he wants instead T *elements and for us to use realloc to constantly adjust the size of the vector. We have not learned about realloc and so I have only the description and example I found here, can someone please tell me why I'm getting an error on line 45 when I try to use this class (also put this topic in beginers because I wasn't sure which board was more appropriate to find help):

#include <iostream>
#include <cstdlib>
using namespace std;

template<typename T>
class MyVector
{
public:
MyVector();
void push_back(T element);
void pop_back();
unsigned int size();
bool empty();
T at(int index);
void clear();
void swap(MyVector v2);

private:
T *elements;
int vectorSize;
};

template<typename T>
MyVector<T>::MyVector()
{
vectorSize = 0;
}

template<typename T>
bool MyVector<T>::empty()
{
return (vectorSize == 0);
}

template<typename T>
T MyVector<T>::at(int index)
{
return elements[index];
}

template<typename T>
void MyVector<T>::push_back(T value)
{
vectorSize++;
elements = (T *)realloc(elements,vectorSize * sizeof(T)); // PROBLEM!
elements[vectorSize - 1] = value;
}
> elements = (T *)realloc(elements,vectorSize * sizeof(T)); // PROBLEM!

What are the possible problems?

If the type T is not trivially constructible, assignable and destructible, you are in deep trouble - I presume that is not the case.

If realloc happens to return a nullptr, then too you are in trouble - I presume that too is not the case.

If vectorSize is negative, then too you are in trouble - that is a possibility.

From the code that you have posted, the error appears to be that the pointer elements is uninitiazed. Your constructor needs to look something like:
1
2
3
4
5
6
template<typename T>
MyVector<T>::MyVector()
{
    elements = nullptr ; // C++98: elements = 0 ;
    vectorSize = 0;
}


You also need to write a copy constructyor, a destructor and an overloaded assignment operator.

And instantiate MyVector<> with only pod types. For example,
1
2
MyVector<int> vec_one ; // ok
//MyVector<std::string> vec_two ; // not ok 


Unhandled exception at 0x771d15de in MyVector.exe: 0xC0000005: Access violation reading location 0xccccccc8.
Hi ,
If the type T is not trivially constructible, assignable and destructible, you are in deep trouble


what does this means ?

1
2
3
4
5
6
7
8
9
10
{
    struct A { /*...*/ } ;
    char memory[ sizeof(A) ] ; 
    A* pa = static_cast<A*>(memory) ; // using pa is ok if and only if A has a trivial (do-nothing) constructor
    
    A object ;
    std::memcpy( pa, &object, sizeof(A) ) ; // ok if and only if A has a trivial copy assignment 
    // ie. objects of type A can be safely copied by  copying the raw bits of the object representation of A

} // at this point life-time of memory is over. ok if and only if A has a trivial (do-nothing) destructor  
Thanks JLBorges ...
Topic archived. No new replies allowed.