Dynamic Memory Error

The following is a class I am writing for a lightweight project. Don't ask why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <new>

template <typename T>
class ArrayList
{
    private:
        unsigned capacity;
        unsigned size;

        T* array;

    public:
        ArrayList(unsigned initCapacity);
        ~ArrayList();

        void add(const T& newElement);
        void add(unsigned index, const T& newElement);

        T remove(unsigned index);

    private:
        void grow();
};

template <typename T>
ArrayList<T>::ArrayList(unsigned initCapacity = 10)
{
    if (initCapacity == 0)
        throw "ArrayList capacity must be greater than 0!";

    size = 0;
    capacity = initCapacity;

    array = new (std::nothrow) T[initCapacity];
    assert(array != 0);
}

template <typename T>
ArrayList<T>::~ArrayList()
{
    delete [] array;
}

template <typename T>
void ArrayList<T>::add(const T& newElement)
{
    assert(capacity > size);

    // insert, and resize if necessary
    array[size++] = newElement;
    if (size == capacity)
        grow();
}

template <typename T>
void ArrayList<T>::add(unsigned index, const T& newElement)
{
    assert(capacity > size);

    if (index > size)
        throw "Addition index cannot be greater than array size!";

    if (index == size)
        add(newElement);
    else
    {
        // shift
        for (int i = size - 1; i >= static_cast<int>(index); --i)
            array[i + 1] = array[i];
        // insert
        array[index] = newElement;

        // resize if necessary
        if (++size == capacity)
            grow();
    }
}

template <typename T>
T ArrayList<T>::remove(unsigned index = 0)
{
    if (index >= size)
        throw "Removal index cannot be greater than array size minus 1!";

    T removed = array[index];
    if (index != size - 1)
        for (int i = index; i <= static_cast<int>(size) - 2; ++i)
            array[i] = array[i + 1];
    --size;

    return removed;
}

template <typename T>
void ArrayList<T>::grow()
{
    capacity *= 2;
    T* newArray = new (std::nothrow) T[capacity];
    assert(newArray != 0);
    for (int i = 0; i < static_cast<int>(size); ++i)
        newArray[i] = array[i];
    delete [] array;
    array = newArray;
}


I have driver code that stresses everything here. A memory checker surrounding this driver program claims that every time delete is called from the destructor or from grow(), I am attempting to free unallocated memory. This makes no sense to me.

Does anyone see what I'm missing?
Last edited on
It seems ok to me as far as memory use is concerned.
is your ArrayList ever being used without using the constructor you defined?

I.E.
1
2
ArrayList al;
al = ArrayList(14);


This would cause the unallocated memory error to occur.

I think it matters (can't be positive without testing it), but try putting your initializer in the ArrayList constructor's declaration rather than definition...
1
2
3
4
5
6
7
8
9
10
class ArrayList
{
.....
ArrayList( unsigned int size = 10 );
}

ArrayList::ArrayList(unsigned int size)
{
...
}
Oh yes, and copy/assignment need to be dealt with.
Hey all,

Thanks for the suggestions. When I stopped passing nothrow to new, the problem went away entirely. So that's that.

Any idea why nothrow would change allocation behavior? I suspect this has something to do with my particular platform and/or memory checker, though.
Topic archived. No new replies allowed.