Code works even though it maybe shouldn't

Hi everyone. I have an assignment, which is to Design and Implement a Stack Class such that if the Stack gets full (default size = 100), it doubles the size of its Dynamically Allocated Array, copies the data into the newly allocated larger array, and releases the old array. Please note that the stack should be intact and contain the element that was to be pushed onto the stack when it ran out of memory. Display all the elements on the stack by popping until the stack is empty. The stack is empty now but it has memory to hold 100 elements,push 250 pseudo random integ er elements onto the stack. The memory should get doubled twice; once when the 101st element is being pushed, and then when the 201st one comes along. 5. Put a “cout statement” in the appropriate method to flag when the memory is getting Doubled.


I've been able to fill the stack with 250 numbers, and it doubles and everything works fine. But I have a feeling I'm doing something , notably in my push function. I kinda just made up some code and it worked . but can you tell if it contains a bug or I'm actually right?


#include <cstdlib>
#include <iostream>

using namespace std;

#define DEFAULT_STACK_SIZE 100
#define SECOND_TEST_SIZE 250

class Stack {

public:
Stack();
Stack(int);
Stack(const Stack& copyee);
Stack& operator =(const Stack& rightElement);
void push(int Value);
void pop();
~Stack();
//private functions are left public for testing purposes (i.e. to show stack size etc. im kinda lazy to make accesor and mutator functions lol)
//private:

int *stackPtr;
int top;
int size;
bool isEmpty() const {return -1 == top;}
bool isFull() const {return size == top;}
};

int main()
{
Stack list; //initializes the stack to empty but with 100 memory //slots(i.e.array)

list.push(185); // pushing 185 random integers onto the stack of size 100 //(so it should get double in the function)

//just shows the index of the top variable on the stack, the stack size, nd last //element (normally would be private though)
cout << "stack position, stack size, last element in stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
list.pop();

cout << "stack position, stack size, value of data in that stack";
cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
return 0;
}
//default constructor initializes the variable to 100;
Stack::Stack()
{
size = DEFAULT_STACK_SIZE;
top = -1;
stackPtr = new int[size];
}
//constructor to set to required size
Stack::Stack(int newsize)
{
size = newsize;
top = -1;
stackPtr = new int[size];
}

//copy constructor
Stack::Stack(const Stack& copyee)
{
size = copyee.size;
top = copyee.top;
stackPtr = new int[size];
for (int i =0; i<top; i++)
stackPtr[i] = copyee.stackPtr[i];
}
//destructor
Stack::~Stack(){
delete [] stackPtr;
cout << endl << "destructor";
}
//= operator overloaded, commented out bcoz code works w/o it
/*Stack& Stack::operator=(const Stack& rightElement)
{
if (size != rightElement.size)
{
delete [] stackPtr;
stackPtr = new int[rightElement.size];
}

size = rightElement.size;
top = rightElement.top;
for (int i=0; i< top; i++)
stackPtr[i] = rightElement.stackPtr[i];

return *this;
}*/

void Stack::push(int numbers2bpushed)
{
for (int i =0; i<= numbers2bpushed; i++)
{
stackPtr[top++] = rand()/100;
//im not sure i understand this loop even though i wrote it, how does it keep my //data intact
if (isFull())
{
Stack* newobj;
newobj = new Stack[size+=100];
delete [] stackPtr;
cout << endl << "Doubling " << endl;
}
}
}

//remove from stack
void Stack::pop()
{
while (!isEmpty())
{
cout << stackPtr[top] << endl;
top--;
}
}





p.s. lost a couple good hours of sleep trying to figure out if there is a bug so any help is greatly appreciated.
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
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cstdlib>
#include <iostream>

using namespace std;

#define DEFAULT_STACK_SIZE 100
#define SECOND_TEST_SIZE 250

class Stack 
{
public:
	Stack();
	Stack(int);
	Stack(const Stack& copyee);
	Stack& operator =(const Stack& rightElement);
	void push(int Value);
	void pop();
	~Stack();
	
	//private functions are left public for testing purposes (i.e. to show stack size etc. im kinda lazy to make accesor and mutator functions lol)
	//private:
	
	int *stackPtr;
	int top;
	int size;
	bool isEmpty() const {return -1 == top;}
	bool isFull() const {return size == top;}
};

int main()
{
	Stack list; //initializes the stack to empty but with 100 memory //slots(i.e.array)
	
	list.push(185); // pushing 185 random integers onto the stack of size 100 //(so it should get double in the function)
	
	//just shows the index of the top variable on the stack, the stack size, nd last //element (normally would be private though)
	cout << "stack position, stack size, last element in stack";
	cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
	list.pop();
	
	cout << "stack position, stack size, value of data in that stack";
	cout << list.top << " " << list.size << " "<< list.stackPtr[list.top] << endl;
	return 0;
}

//default constructor initializes the variable to 100;
Stack::Stack()
{
	size = DEFAULT_STACK_SIZE;
	top = -1;
	stackPtr = new int[size];
}
//constructor to set to required size
Stack::Stack(int newsize)
{
	size = newsize;
	top = -1;
	stackPtr = new int[size];
}

//copy constructor
Stack::Stack(const Stack& copyee)
{
	size = copyee.size;
	top = copyee.top;
	stackPtr = new int[size];
	for (int i =0; i<top; i++)
	stackPtr[i] = copyee.stackPtr[i];
}

//destructor
Stack::~Stack()
{
	delete [] stackPtr;
	cout << endl << "destructor";
}

//= operator overloaded, commented out bcoz code works w/o it
/*Stack& Stack::operator=(const Stack& rightElement)
{
if (size != rightElement.size)
{
delete [] stackPtr;
stackPtr = new int[rightElement.size];
}

size = rightElement.size;
top = rightElement.top;
for (int i=0; i< top; i++)
stackPtr[i] = rightElement.stackPtr[i];

return *this;
}*/

void Stack::push(int numbers2bpushed)
{
	for (int i =0; i<= numbers2bpushed; i++)
	{
		stackPtr[top++] = rand()/100;
		
		//im not sure i understand this loop even though i wrote it, how does it keep my //data intact
		if (isFull())
		{
			Stack* newobj;
			newobj = new Stack[size+=100];
			delete [] stackPtr;
			cout << endl << "Doubling " << endl;
		}
	}
}

//remove from stack 
void Stack::pop()
{
	while (!isEmpty())
	{
		cout << stackPtr[top] << endl;
		top--;
	}
}


Please do post your code within code tag next time.
Topic archived. No new replies allowed.