简体   繁体   中英

resize array of pointers c++

Let's say that I have this

#define T 10

StackOverflow *_stObject[H];

How can I "resize" this array to 20? I cannot use vectors. I have to copy that 11 positions to another array of pointers with 20 positions, but that way I will cannot use this object in my other functions.

This object stores data, and when it gets full I need to find a way to continue to add data.

How can I do this?

Ok here it is some more information because it's not working. I made the method extendArray() but it have an error when I make r = temp

This is a calculator and the class Reg stores the information the operation that I make in the calculator. The object "r" stores 10 operations and I have to expand this array of pointers if I make more than 10 operations.

The error message that I get is in r = temp and it says: incompatible types in assignment of 'Reg* [20]' to 'Reg* [10]'|

#define T 10

   class Calculator
    {
        public:
            Calculator();
            Calculator(NumComp&,NumComp&);
            ~Calculator();

            void printVisor();
            void setCalculator(const NumComp&,const NumComp&);
            void menu();
            void help();
            void clean();
            void run();
            void extendArray();

        private:
            NumComp n1, n2;
            Reg *r[T];
            int _tMax;
    };

        void Calculator::extendArray()
    {
        Reg *temp[T*2];

        for(int i = 0; i < 5; i++){
            temp[i] = new Reg;
            temp[i] = r[i];
          delete r[i];
         }

        r = temp;
        _tMax *= 2;
    }

You have to create a new array and do a deep copy.

 StackOverflow * array = new StackOverFlow[(H * 2)];

 for (int i = 0; i < H; i++)
       arrary[i] = _stObject[i];

Now you have an array twice the size of the original with all the data from the original.

If your assignment/homework states you cannot use any STL containers you can do something like this

#define T 10

class NumComp {
// ---
};

class Reg {
// ---
};


class Calculator
{
     public:
         Calculator();
         Calculator(NumComp&,NumComp&);
         ~Calculator();

         void printVisor();
         void setCalculator(const NumComp&,const NumComp&);
         void menu();
         void help();
         void clean();
         void run();
         void extendArray();

     private:
         NumComp n1, n2;
         Reg** r;
         int _tMax;

         void createArray();
         void cleanupArray(); 
};


Calculator::Calculator() {
    createArray();
}

Calculator::Calculator(NumComp&,NumComp&) {
    createArray();
}

Calculator::~Calculator() {
    cleanupArray();
}

void Calculator::createArray() {

    // create a pointer array
    r = new Reg*[T];

    // initialize to nulls
    for (int i = 0; i < T; i++) {
        r[i] = 0;
    }

    _tMax = T;
}

void Calculator::cleanupArray() {

    // make sure the contents of r[] are deleted by whoever allocates them
    delete [] r;
    r = 0;
}


void Calculator::extendArray()
{
    Reg** temp = new Reg*[_tMax*2];

    // copy contents of r to temp
    for(int i = 0; i < _tMax; i++) {
        temp[i] = r[i];
    }

    // initialize rest of temp
    for (int i = _tMax - 1; i < _tMax*2; i++) {
        temp[i] = 0;
    }

    // delete what was originally in r
    delete [] r;
    r = temp;

    _tMax *= 2;
}

The only way to extend an array is to copy its content to a larger array. You cannot simply assign a larger array to a smaller array (That's why you got the error incompatible types in assignment of 'Reg* [20]' to 'Reg* [10]' ). But you can assign a pointer-to-an-array of any size to another pointer-to-an-array (of same type). So to extend an array, you first create a larger array, copy contents of smaller array to it, clean up the smaller array, assign the pointer to the larger array to the smaller array.

If you can't use the std::vector (which would be the most reasonable thing to use), then you could write your own "vector" with simplified functionality:

class MyVector
{
    int size;
    int capacity;
    StackOverFlow ** data;

    public:

    MyVector() : size(0), capacity(10), data(new StackOverFlow * [10]) {}
    ~MyVector() {delete data;}

    void duplicateCapacity()
    {
        capacity *= 2;
        StackOverFlow ** tmp = new StackOverFlow * [capacity];
        for(int i=0; i<size; i++)
             tmp[i] = data[i];
        delete data; //<-- here was an error...
        data = tmp;
    }

    void add(StackOverFlow * item)
    {
        if(size == capacity)
            duplicateCapacity();
        data[size++] = item; 
    }

    StackOverFlow * get(int index) {return data[index];}
};

With this basic functionality you could get the job done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM