0

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;
    }
wormwood87
  • 153
  • 1
  • 3
  • 12
  • 4
    You cannot. You will have to describe requirements that can be met if you want an actual working solution. – R. Martinho Fernandes Nov 12 '12 at 20:35
  • 3
    Why can't you use `std::vector`? – Joseph Mansfield Nov 12 '12 at 20:35
  • Arrays are static data structures therefore you cannot resize them at runtime. – Robin Chander Nov 12 '12 at 20:36
  • @Fernandes: I have this array of pointers where I need to store data, and when the array is full I have to "resize it" to continue to store data. – wormwood87 Nov 12 '12 at 20:38
  • @sftrabbit: I haven't learn that yet, I have a big project that is 95% done, this is the only thing that I haven't done yet. – wormwood87 Nov 12 '12 at 20:40
  • @evanmcdonnal: I know that we cannot resize an array, i should have say "resize", a way to add more data to a array that is full. – wormwood87 Nov 12 '12 at 20:42
  • To resize the array at run time you will have to make a new array of size 20. Copy the old values into the new array and delete your old array then make your old array pointer point to the new array. – andre Nov 12 '12 at 20:42
  • @ahenderson: I did something like that about 1 hour ago, but maybe I forgot about a small detail or something because it didn't worked and I thought that I was doing it the wrong way, I will try to do it again now that I know that is the right way. – wormwood87 Nov 12 '12 at 20:58
  • If there is no place in your code the *value* of stObject is kept with persistence outside of the variable declared here (i.e. it is only used; never "remembered") then this is easily doable. – WhozCraig Nov 12 '12 at 22:02

3 Answers3

1

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.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 1
    Arrays can't be re-sized (they are defined at compile time). If you are dynamically allocating memory (like this) then there are better techniques in C++. `std::vector`. Also in the question above it is actually an array of pointers not objects so your comparison is also of. – Martin York Nov 12 '12 at 20:43
  • 1
    @LokiAstari For some reason I thought he couldn't use `std::vector` I misread the comments. – evanmcdonnal Nov 12 '12 at 20:47
  • @LokiAstari Additionally I'm just making assumptions (potentially stupid ones) but when I was taking these classes you were not allowed to use the standard library for anything. The idea was to make you learn how the abstraction works, not just how to use it. – evanmcdonnal Nov 12 '12 at 20:54
  • No, you're not misreading the comments. He said he "cannot use vectors". – Pete Becker Nov 12 '12 at 20:57
  • 1
    I don't think this teaches how the abstraction works. This teaches how to dynamically allocate arrays. The abstraction in question (`std::vector`) is not just that (probably the most important thing it does is automatic memory management). – R. Martinho Fernandes Nov 12 '12 at 21:00
0

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.

ubi
  • 4,041
  • 3
  • 33
  • 50
-1

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.

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • if you see any problems, feel free to correct it, i currently cannot use an ide, so i wrote the code in the StackOverflow editor above :D – Balázs Édes Nov 12 '12 at 22:47
  • Uh-oh, this does not take into account the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). That means it will stop working very easily (all that needs to happen is for it to be passed or returned by value). – R. Martinho Fernandes Nov 13 '12 at 08:51
  • 1
    Welcome to UB by `delete`ing something you `new[]`d. And doesn't deal with Rule of Three properly, and is also not templated. – Puppy Nov 13 '12 at 15:15
  • 1
    Like I said, I made a few changes and this solution worked to solve my problem. I'm using only one method, I didn't needed the add method because I have an if that calls the funcion to duplicateCapacity only when size == capacity. I've making tests and this solution is working perfectly. I cannot use templetes or vectors because I only have been learning C++ for about 1 month. – wormwood87 Nov 13 '12 at 18:49