creating a new array


hi im trying to add mode memory in this stack function after it become full.
i want to use this loop:

for (int i = 0; i < myCapacity; i++)
temp[i] = myArray [i];

the fucntion is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Stack::push(const StackElement & value)
{
   if (myTop < myCapacity - 1) //Preserve stack invariant
   { 
      ++myTop;
      myArray[myTop] = value;
   }
   else
   {
      cerr << "*** Stack full -- can't add new value ***\n"
              "Must increase the stack's capacity.\n";
      exit(1);
   }
}


also should i create this array :
temp = new stackElement[myCapacity + 10] in push function???
Basically. Use new to create the new array, copy the existing elements over, adjust the capacity, and delete the old array. Make sure the class's internal pointer is pointing to the new one as well.
so all those steps are suppose to be made in push function??? or i would have to change the pop function???

tanks
Yes, if the array is full and needs to be extended. You could make it a separate function if you liked, so the user could extend the capacity manually if they wanted.
can you give me an abstract example of what you saying????
If you are allowed to use C++ STL, then a vector class will solve your problem. It will handle auto-expand to take in more items once it reach the limit.
i know but im trying to do it without using vector.
Well then Zhuge has given you the solution conceptually. Or you are looking for example code to do that is it ?
yes, an example code just so i can understand it better
Since I don't have the whole class code below is snippet not tested but can start you rolling.

1
2
3
4
5
6
7
8
9
cerr << "*** Stack full -- can't add new value ***\n"
              "Must increase the stack's capacity.\n";

//in here myTop == myCapacity
StackElement[] tempArray = new StackElement[myTop*2];
for(int i=0;i<myTop;i++) tempArray[i] = myArray[i]; //assume StackElement has proper assignment operator defined
delete [] myArray;
myArray = tempArray;
myCapacity = myTop*2;


it seems that i wont need to do some adjustment on my top function.
here my top function implementation

1
2
3
4
5
6
7
8
9
10
11
StackElement Stack::top() const
{
   if ( !empty() ) 
      return (myArray[myTop]);
   else
   {
      cerr << "*** Stack is empty -- returning garbage value ***\n";
      StackElement garbage;
      return garbage;
   }
}


here is the new array i want to implement temp = new stackElement[myCapacity + 10]

therefore it would be

mycapacity = mycapacity + 10; right??
My suggestion was referring to the push function instead.

Yes you do a myCapacity += 10; But some implementations like to do a myCapacity*=2; to give more space to prevent extending of your internal array from occurring too often. If you see the code, the operations are not cheap whenever you do a extend operation. In exchange for wasting some memory, you gain better performance for your Stack class.

this was the way i implemednt your code on push stack, but it didnt work. when i type (for example) 123 as a capacity, then i type 124 as element it says stack full, but it does not increase 10 more capacity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Stack::push(  const StackElement & value)
{	
   if (myTop < myCapacity - 1) //Preserve stack invariant
   { 
      ++myTop;
      myArray[myTop] = value;
	  
   }
   

   else
   {		cerr<<" the stack is full. creating new memory"<<endl;
   

		temp = new StackElement[myCapacity + 10];
		for(int i=0;i<myTop;i++) 
		temp[i] = myArray[i]; //assume StackElement has proper assignment operator defined
		delete [] myArray;
		myArray = temp;
		myCapacity = myCapacity += 10;
		

   }
      
   exit(1);
}
then i type 124 as element it says stack full, but it does not increase 10 more capacity


How do you know it does not increase 10 more capacity ? There is one more code missing from above.

1
2
3
4
5
6
7
temp = new StackElement[myCapacity + 10];
for(int i=0;i<myTop;i++) 
  temp[i] = myArray[i]; //assume StackElement has proper assignment operator defined 
delete [] myArray;
myArray = temp;
myArray[myTop] = value; //add this line in
myCapacity = myCapacity + 10; //change operator a bit here 
Last edited on
closed account (D80DSL3A)
Still one more thing. myTop wasn't incremented. It looks like it should be incremented before the assignment on line 6. A pre-increment should do it.
myArray[++myTop] = value; //add this line in

Edit: Isn't the for loop upper limit one short? myTop = highest index value.
Last edited on
Now you bring this up, I am wondering if the OP want a 0-based or 1-based array indexing.

1
2
3
4
5
6
   if (myTop < myCapacity - 1) //Preserve stack invariant
   { 
      ++myTop;
      myArray[myTop] = value;
	  
   }


If right at the beginning when array is empty and adopt 0-based indexing, it should be

1
2
3
4
5
6
7
//0-based indexing
myArray[myTop] = value;
++myTop;

//1-based indexing
++myTop;
myArray[myTop] = value;


But his code implies 1-based indexing and that is increment myTop FIRST and then assignment. Yet where he check it is if (myTop < myCapacity - 1) which seem to implies he is doing the check based on 0-based indexing approach but seems funny why < myCapacity - 1?

If 0-based the check should be if (myTop < myCapacity)
If 1-based the check should be if (myTop <= myCapacity)

So I am not sure the OP want 0-based or 1-based or he want BOTH ?!?!
why < myCapacity - 1 the reason, it implies that my capacity is full (-1)

anyway, now im trying to create a new code for my driver menu so that i can use that new array manually after the another one gets full;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int cap, capa;
   cout << "Enter stack capacity: ";
   cin >> cap;

   Stack s(cap);
   cout << "Stack created.  Empty? " << boolalpha << s.empty() << endl;

   cout << "How many elements to add to the stack? ";
   int numItems;
   cin >> numItems;
   for (int i = 1; i <= numItems; i++) 
      s.push(100*i);
   
   cout << "Stack empty? " << s.empty() << endl;
   

   cout << "Contents of stack s (via  print):\n";
   print(s); cout << endl;
  
   cout << "Check that the stack wasn't modified by print:\n";
   s.display(cout); cout << endl;
 

   Stack t, u;
   t = u = s;
   cout << "Contents of stacks t and u after t = u = s (via  print):\n";
   cout << "u:\n"; print(u); cout << endl;
   cout << "t:\n"; print(t); cout << endl;

   cout << "Top value in t: " << t.top() << endl;

   while (!t.empty())
   {
     cout << "Popping t:  " << t.top() << endl;
     t.pop();
   }
   cout << "Stack t empty? " << t.empty() << endl;
   cout << "\nNow try to retrieve top value from t." << endl;
   cout << "Top value in t: " << t.top() << endl;
   cout << "\nTrying to pop t: " << endl;
   t.pop();
}


Topic archived. No new replies allowed.