3

Disclaimer: Yes, I know about std::vector. I'm doing this for the sake of learning.

I'm working on making a dynamic array class, and I'm trying to get add to work.

template <class T>
void Array<T>::add(T value)
{
    T * tmp = new T[mCount];

    for (int i = 0; i < mCount; i++)
    {
        tmp[i] = mData[i];
    }

    mCount++;

    delete[] mData;
    mData = tmp;

    mData[mCount - 1] = value;
}

It works... sort of. The function works in adding the element, but then the program crashes when exiting. No errors, no nothing. It just freezes, and I have to close it using (Shift + F5).

So, what's wrong with this?

Here's my whole class. If I didn't include a function it means there's no code in it.

#ifndef ARRAY_H
#define ARRAY_H

using namespace std;

template <class T>
class Array
{
private:
    T * mData;
    int mCount;

public:
    Array();
    ~Array();

    void add(T value);
    void insert(T value, int index);
    bool isEmpty();
    void display();
    bool remove(T value);
    bool removeAt(int index);
    int size();

    T & operator[](const int index);
};

// Constructors / Destructors
// --------------------------------------------------------

template <class T>
Array<T>::Array()
{
    mCount = 0;
    mData = new T[mCount];
    for (int i = 0; i < mCount; i++)
        mData[i] = 0;
}

template <class T>
Array<T>::~Array()
{
    delete[] mData;
}

// General Operations
// --------------------------------------------------------

template <class T>
void Array<T>::add(T value)
{
    T * tmp = new T[mCount];

    for (int i = 0; i < mCount; i++)
    {
        tmp[i] = mData[i];
    }

    mCount++;

    delete[] mData;
    mData = tmp;

    mData[mCount - 1] = value;
}

template <class T>
void Array<T>::display()
{
    if (isEmpty())
    {
        cout 
            << "The array is empty."
            << "\n\n";
        return;
    }

    cout << "(";

    for (int i = 0; i < mCount; i++)
    {

        cout << mData[i];

        if (i < mCount - 1)
            cout << ", ";
    }

    cout << ")" << "\n\n";
}

template <class T>
bool Array<T>::isEmpty()
{
    return mCount == 0;
}

template <class T>
int Array<T>::size()
{
    return mCount;
}

// Operator Overloads
// --------------------------------------------------------

template <class T>
T & Array<T>::operator[](const int index)
{
    return mData[index];
}

#endif

If you need any additional info lemme know and I can post it.

8
  • 1
    OT, but why not use std::vector? Commented Oct 10, 2015 at 22:42
  • 1
    @MrTux, for educational purpose Commented Oct 10, 2015 at 22:43
  • 1
    Surely you need mcount + 1 in the array declaration to avoid an array out of bounds exception Commented Oct 10, 2015 at 22:43
  • 3
    The first error is in add function. Change the code to T * tmp = new T[mCount + 1]; Commented Oct 10, 2015 at 22:44
  • 1
    What's the invariant of mCount? Commented Oct 10, 2015 at 22:44

1 Answer 1

3

Assuming mCount keeps the number of elements in the array, then when adding a new element you really have to allocate at least mCount + 1 elements (assuming of course you want to keep all the old ones and the new one) via:

T * tmp = new T[mCount + 1];

as opposed to:

T * tmp = new T[mCount];

If it's for anything else other than educational purposes, please use std::vector instead. For example your add function is not exception safe.

Sign up to request clarification or add additional context in comments.

6 Comments

In all reality one should probably allocate 2*mcount elements.
Wow, thanks. I love when the solution is right under my nose but I can't see it. Once it let's me accept your answer I will.
I would rather move incrementation of mCount to the beginning of the function.
@Zereges Probably want to keep the old value until you have a good state again. What if the alloc fails?
@PeterSchneider Well, if alloc fails, you are pretty much screwed anyway :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.