ArcheAngel
ArcheAngel

Reputation: 35

Resizing dynamically allocated pointer array to class

How would you resize an array without using vectors? The array I want to resize is a pointer to a class

class A
{
    private:
    B * b;
    int numOfItems;
    int maxNumOfItems;

    public:
    A();
    ~A();
    resizeMX();
};


A::A()
{
     numOfItems = 0;
     maxNumOfItems = 20;
     b = new B[maxNumOfItems];
     for(int i=0;i<maxNumOfItems ;i++)
     {
         b[i] = 0;
     }
}

A::~A()
{
    for(int i=0;i<numOfItems;i++)
     {
         delete [] b;
     }
}

void A::resizeMX(const B & obj)
{
     bool value=false;
     if(numOfItems<=maxNumOfItems && value == false)
     {
        //assign values to *b in for loop
     }
     else
     {
       //resize index of *b 

I know that we are supposed to dynamically allocate new memory. Something like this?

       ++maxNumOfItems; 
        b=new B[maxNumOfItems];
        //keep previous assigned values and add new values at the end
        for(int j=numOfItems;j<maxNumOfItems;j++)
        {
            //assign values to b[j]
        }
     }  
        numOfItems++;
}

assume that I did overload the = operator

Upvotes: 1

Views: 10968

Answers (3)

Peter
Peter

Reputation: 36597

Firstly, an array is not a pointer. A pointer can contain the address of a first element of an array, which may or may not be dynamically allocated.

Anyway, assuming you have a pointer to a dynamically allocated array of B

pointer = new B[size];   //  assume size > 0

  //  optionally do something with the elements of pointer other than resizing

then, to resize it is necessary to dynamically allocate a new array

B *new_pointer = new B[newsize];    // assume size < newsize

then copy elements to the new array

for (i = 0; i < size; ++i)
      new_pointer[i] = pointer[i];     // relies on B's assignment operator

and then release the old dynamically allocated array

delete [] pointer;

and, lastly, reassign pointer to point at the newly allocated array.

pointer = new_pointer;

A standard container is preferable, since it manages all the allocations and reallocations as needed.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

When you do your reallocation b = new B[...] you lose the original pointer, and therefore all previous objects.

Instead you need to use a temporary variable for the new memory, and copy (or move) from the old b to the new memory. Then you delete the old memory and reassign the pointer,

Upvotes: 0

Martin Gardener
Martin Gardener

Reputation: 1001

You cannot resize array, you can only allocate new one (with a bigger size) and copy old array's contents. If you don't want to use std::vector (for some reason) here is the code to it:

int size = 10;
int* arr = new int[size];

void resize() {
    size_t newSize = size * 2;
    int* newArr = new int[newSize];

    memcpy( newArr, arr, size * sizeof(int) );

    size = newSize;
    delete [] arr;
    arr = newArr;
}

Upvotes: 7

Related Questions