doubling the size of an array C++

  • Ponzzz
  • Born
  • Born
  • Ponzzz
  • Posts: 3

Post 3+ Months Ago

I have an assignment to create an array of integers that behaves as a Stack. all of my push, pop functions work perfectly fine but im running into a problem when I try to double the size of my array(this happens when I try to push on a full array). Here's the code I have so far...

From header file:

Code: [ Select ]
class Stack
{
    public:
        int size, front, back;

        void createStack(int);
        void push(int);
        void extendSize();
        void printElements();
        int pop();
        int getSize();
        bool isEmpty();
        bool isFull();

    private:
        int* stackArray;
        int* newArray;

};
  1. class Stack
  2. {
  3.     public:
  4.         int size, front, back;
  5.         void createStack(int);
  6.         void push(int);
  7.         void extendSize();
  8.         void printElements();
  9.         int pop();
  10.         int getSize();
  11.         bool isEmpty();
  12.         bool isFull();
  13.     private:
  14.         int* stackArray;
  15.         int* newArray;
  16. };


The function im trying to implement:
Code: [ Select ]
void Stack::extendSize()
{
    newArray = stackArray;
    stackArray = new int[size*2];
    stackArray = newArray;
    size *= 2;
}
  1. void Stack::extendSize()
  2. {
  3.     newArray = stackArray;
  4.     stackArray = new int[size*2];
  5.     stackArray = newArray;
  6.     size *= 2;
  7. }


extendSize() does WORK but when I print out the elements in the stack it looks something like this:

Index 0 = 3
Index 1 = 3
Index 2 = 3
Index 3 = 3
Index 4 = 3
Index 5 = 3
Index 6 = 3
Index 7 = 3
Index 8 = 3
Index 9 = 3
Index 10 = 3
Index 11 = -1414812757
Index 12 = -1414812757
Index 13 = -17891602
Index 14 = 0
Index 15 = 0
Index 16 = 1522491621
Index 17 = 469763407
Index 18 = 5001472
Index 19 = 5000384

The elements at the end aren't set to 0 so I eventually get a corruption in the heap. Any advice?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I would create your new array, run through it and initialize all values to 0 and then copy each value over or something similar.

Code: [ Select ]
void Stack::extendSize()
{
    newArray = new int[size*2];
    for(i=0;i<size*2;i++)
    {
      if(i<size)
      {
        newArray[i] = stackArray[i];
      }
      else
      {
        newArray[i] = 0;
      }
    
    stackArray = newArray;
    size *= 2;
}
  1. void Stack::extendSize()
  2. {
  3.     newArray = new int[size*2];
  4.     for(i=0;i<size*2;i++)
  5.     {
  6.       if(i<size)
  7.       {
  8.         newArray[i] = stackArray[i];
  9.       }
  10.       else
  11.       {
  12.         newArray[i] = 0;
  13.       }
  14.     
  15.     stackArray = newArray;
  16.     size *= 2;
  17. }
  • Ponzzz
  • Born
  • Born
  • Ponzzz
  • Posts: 3

Post 3+ Months Ago

ahh now I see how it works, I definitely know what to do now. Thank you very much!

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 172 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.