0

I'm working on an assignment right now and have run into a roadblock. The assignment is an array list in C++ that dynamically expands by a factor of 2 every time it runs out of room to store new elements (initially starts with room for 2 elements). Here is the code I'm working on (some of it is included in a separate .h file provided by the professor, I won't post everything in order to keep this compact).

#include "array_list.h"

//initial size to create storage array
static const unsigned int INIT_SIZE = 2;
//factor to increase storage by when it gets too small
static const unsigned int GROW_FACTOR = 2;
unsigned int    growthTracker = 1;

array_list::array_list()
{
    m_storage = new unsigned int[INIT_SIZE];
    m_capacity = INIT_SIZE;
    m_current = -1;
    m_size = 0;
}

array_list::~array_list()
{
    delete m_storage;
}

void array_list::clear()
{
    delete m_storage;
    m_storage = new unsigned int[INIT_SIZE];
    m_capacity = INIT_SIZE;
    m_current = -1;
    m_size = 0;
}

unsigned int array_list::size() const
{
    return m_size;
}

bool array_list::empty() const
{
    bool A = 0;
    if(m_size == 0)
    {
        A = 1;
    }
    return A;
}

void array_list::insert(const unsigned int val)
{
    m_storage[m_size++] = val;
    m_current = m_size;
}

void array_list::grow_and_copy()
{
    if(m_size == m_capacity)
    {
        new unsigned int[INIT_SIZE * (GROW_FACTOR ^ growthTracker)];
        growthTracker++;
        m_capacity = m_capacity * 2;
    }
    m_storage[m_size++] = val;
}

Now, my problem is trying to figure out how to copy the values of the old, smaller array into the new, larger one. If I wasn't using dynamic unnamed arrays, this would be very easy to do with a loop, a simple case of "for a certain range, arrayA[i] = arrayB[i]." However, because the arrays are just defined as new unsigned int[], I'm not sure how to go about this. There are no names, so I can't figure out how to tell C++ which array to copy into which. And since the grow_and_copy could be called multiple times, I'm fairly sure I can't give them names, right? Because then I would end up with multiple arrays with the same name. Can anyone point me in the right direction here? Thanks so much.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Mock
  • 359
  • 1
  • 3
  • 11
  • m_storage has to be declared somewhere, but I'm not finding it. It's probably in your `array_List.h` file – Sam I am says Reinstate Monica Oct 13 '14 at 21:44
  • but look how `m_storage` is instantiated. `m_storage = new unsigned int[INIT_SIZE];` the `new` keyword returns your array, and you have to assign it to a variable – Sam I am says Reinstate Monica Oct 13 '14 at 21:46
  • If you've already declared `m_storage` in your `.h` file (presumably as an `unsigned int*`), just make your grow function declare a temporary variable of type `unsigned int*` and assign it a pointer to your new array, copy from `m_storage` into the new array, delete `m_storage`, and then assign `temp` to `m_storage`. This is assuming you aren't using C++11 smart pointers, of course – IllusiveBrian Oct 13 '14 at 21:47
  • I'll clarify: Yes, m_storage is declared in the .h file as unsigned int * . – Mock Oct 13 '14 at 21:50
  • okay, so instead of `new unsigned int[INIT_SIZE*(GROW_FACTOR^growthTracker)];`, use `unsigned int* newArray = new new unsigned int[INIT_SIZE*(GROW_FACTOR^growthTracker)];`, and you have a name for your new array – Sam I am says Reinstate Monica Oct 13 '14 at 21:51

1 Answers1

0
array_list::growList(int increase = GROW_FACTOR)
{
    unsigned int* temp = m_storage;
    m_storage = new unsigned int[m_capacity * increase];
    for (int i = 0; i < m_capacity; i++)
        m_storage[i] = temp[i];
    m_capacity *= increase;
    delete temp;    
}

I don't know if there are other variables you want to change, but this should basically do what you are asking.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
  • Allocate into temporary then move over to generate better exception/early exit safety. As it stands, if `new` throws, capacity disagrees with the pointer's capacity. – Yakk - Adam Nevraumont Oct 13 '14 at 22:26
  • This is excellent, thank you. I couldn't quite wrap my brain around the pointer use, this was EXACTLY what I needed to set me on track! – Mock Oct 13 '14 at 22:28
  • @Yakk That's a good point, but I get the feeling that if new throws the program is intended to exit anyway. I'll change it though. – IllusiveBrian Oct 13 '14 at 22:31