简体   繁体   中英

c++ memory allocation and rewriting the vector class

I'm doing an assignment in c++ where we have to rewrite the std::vector class that inherits from an abstract Container class. We have to write methods for 'Vector' such as begin(), end(), push_back(), etc... Many of the basic std::vector Member functions found here: http://www.cplusplus.com/reference/vector/vector/


My question is multi-faceted mainly because I am a beginner and the memory of computers is fascinating. Also, sorry if this question is super specific or has similar answers on here but I'm really stuck because this is the first time I've dealt with memory.


Here are my Container and Vector class implementations:

template <typename T> 
class Container {
    protected:
        T* elem;
        int cap;
    public:
        virtual T& operator[](int i) = 0;
        virtual int size() = 0;
        ~ Container() { delete[] elem; }
};

template <typename T>
class Vector: public Container<T> {
    protected:
        using Container<T>::elem;
        using Container<T>::cap;
    private:
        int sz;
    public:
        // constructors
        Vector(); // default constructor
        Vector(int n); // fill constructor #1
        Vector(int n, T fill_val); // fill constructor #2
/*      // ~ Vector(); // destructors
unsure if the above is correct, because of the Container destructor                                
*/  
        // overloaded operators
        T& operator[](int i); // subscript overloading '[]'
        T& operator=(const Vector<T>& V); // assignment overloading '='
        // access methods
        T& front(); //returns a reference to the first element
        T& back(); //returns a reference to the last element
        // iterator methods
        T* begin(); // returns memory address of the first element
        T* end(); // returns memory address at the end
        // size methods
        int size() { return sz; }
        int capacity() { return cap; }
};

Part 2.

When/why do I need the destructor? I've tried to read up on destructors on Stack, but I feel more confused than clarified. I'm assuming the SegFault is coming from memory needing to be deallocated, and I know I'm supposed to be learning how to dynamically allocate memory with this assignment. The constructors and destructors are crucial for this process but can someone break it down as simply as possible for me?


EDIT:

I've fixed the segmentation fault by implementing Fureeish's answer. My code has been updated above.

The only remaining inquiry is to the destructor and its purpose. Do I need to implement one for Vector or will it be called automatically by Container ?

You decided to manage the memory by yourself, but you never allocated any. You're missing some new s here and there. For example, given a:

Vector(int n, T fill_val) {
    sz = n;
    cap = 2*n;
    for(int i = 0; i < sz; i++) {
        elem[i] = fill_val; 
    }
}

the for() loop iterates over elem , which is an uninitialized pointer. You treat it like a pointer to a dynamically allocated array, but it simply points to some garbage value. Remember - first allocate, then work on ( only when you decide to manage the memory yourself. Typically you should prefer using standard implementations).

The correct version should look like this:

Vector(int n, T fill_val) {
    sz = n;
    cap = 2*n;
    elem = new T[cap]; // or cap-1, depending on what you do with `end()`.
    for(int i = 0; i < sz; i++) {
        elem[i] = fill_val; 
    }
}

The fact that you never encountered this problem while using other constructors is just you being extremely lucky and undefined behavior being sneaky

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM