Expanding an Array

Ok so this is a school exercise so I know a vector would be more appropriate but I’m required to use an array. I’m making a stack implementation using an array. I’m keeping track of the size of the array with the var “cap” and I’m tracking the size of the simulated stack with the var “size”. Where it is erring out at the point when I attempt to push() an item beyond the original array bounds. In the push() method I have an if statement checking the if the “size” (size of the stack) is equal to the cap (size of the array) and if it is I should be creating a new array (call newItems), copying ever thing to the new array, delete the old array to free it up, then recreate it with the new size, and copy the new array (newItems) to the freed up old array. That’s the problem. I think it’s an issue of scope or I failing to recreate it. Ultimately when I attempt to add that last item I get the error.

Also is someone can point out the proper way/place to do the deletes of the arrays that I create with the new opperator that would be great. I'm still hazy on that part.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef _ARRAYSTACK_H
#define _ARRAYSTACK_H

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Stack.h"
using namespace std;

const int DEFAULT_INITIAL_CAP  = 5;
const double DEFAULT_ENLARGE_RATE = 0.2;

template <typename T>
class ArrayStack : public Stack<T> {

public:
	ArrayStack();
	ArrayStack(int argCap, double argEnlargeRate);
	~ArrayStack();	

	bool empty() const;
	T push(T item);
	T pop();
	T peek() const;

private:
	
	T items[DEFAULT_INITIAL_CAP ];
	int size; // size of the stack, not the size of the array.
	int cap; // size of the items array
	double enlargeRate;
};


template <typename T>
ArrayStack<T>::ArrayStack(){
	cap = DEFAULT_INITIAL_CAP;
	enlargeRate = DEFAULT_ENLARGE_RATE;
	size = 0;
}

template <typename T>
ArrayStack<T>::ArrayStack(int argCap, double argEnlargeRate){
		cap = argCap;
		enlargeRate = argEnlargeRate;
		items = new T[cap];
		size = 0;	

}

template <typename T>
ArrayStack<T>::~ArrayStack(){
	//delete arrayStack;
}

template <typename T>
bool ArrayStack<T>::empty() const{
	return (size == 0); 
}

template <typename T>
T ArrayStack<T>::peek() const {


	if (size == 0)
		throw logic_error("Peeking an empty stack");

	return items[size - 1];
}

template <typename T>
T ArrayStack<T>::pop(){

	T result = peek();
	size--;
	return result;
}

template <typename T>
T ArrayStack<T>::push(T item){

	// harder case: array is full.
	if (cap == size){

		int newCap = (int)(cap * (1 + enlargeRate));

		T* newItems = new T[newCap];
		for (int i = 0; i < size; i++){
			newItems[i] = items[i];
		}

		delete [] items;
		T* items = new T[newCap];
		items = newItems;
		cap = newCap;

		delete [] newItems;
	}

	// easier case: array is not full.
	items[size] = item;
	size++;
	return item;

}

#endif 


I'll be on for the night so let me know if anyone wants to see anything else. Like Stack.h file or the test.cpp I'm using.

Thanks for any help I can get
The first problem I see is that you've declared items to be a fixed-sized array. Fixed-size arrays
cannot be resized. You need items to be a pointer to T.

I did not look beyond the class definition as deleting items will definitely corrupt the heap.
Alright my first though was to change the items array declaration to
 
T* items[DEFAULT_INITIAL_CAP ]; 

But that then seems to cause an error down in the copy of the items array to the newItems array (the temp array). The error is that the types don't match. So my first question is if I totaly misunderstood what I was told about the array declaration.

Thanks for the feed back jsmith
T* items[ DEFAULT_INITIAL_CAP ]; still just declares a fixed length array, but this time the
elements are pointers-to-T instead of T. You want T* items;
closed account (jy8CpfjN)
arrays are best use when u go over size, so it is best making the array larger then exact or close to the size thats needed but ur pollymorphim solution can work out if complicating programming was just testing ur own skill level.
Ok I got it now. Rather then have a hard codded array (items[]) I need a pointer and do something like this
 
items = new T[some_number];


I got it working

Thanks so much jsmith

Topic archived. No new replies allowed.