1

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

1
  • You're ~A() should only call the delete once. delete [] b; Commented Sep 11, 2016 at 15:26

3 Answers 3

7

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Using memcpy for C++ objects is usually not a good idea. Instead use e.g. std::copy or loop manually and move the objects.
well it never did anything wrong for me though. I guess its up to preference and experience. I'll still look into std::copy, thanks :) @JoachimPileborg
For a basic type like int, memcpy() works fine. For a class type, as requested by the OP, it (often) gives undefined behaviour.
What's the advantage of using memcpy over realloc?
0

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,

Comments

0

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.

Comments

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.