Im just starting to learn C++ programming and for exercise i found this task. I have to write a dynamic, array based stack. This is what i have got so far.
#include <iostream>
using namespace std;
class CStack
{
private:
char *bottom_;
char *top_;
int size_;
public:
CStack(int n = 20){
bottom_ = new char[n];
top_ = bottom_;
size_ = n;
}
int getSize(){ return size_; }
void push(char c){
if (!full()){
*top_ = c;
top_++;
}
else{
resize(size_ * 2);
*top_ = c;
top_++;
}
}
void resize(int newSize){
//Allocate new array and copy in data
char *newArray = new char[newSize];
memcpy(newArray, bottom_, size_);
//Delete old array
delete[] bottom_;
//Swap pointers and new size
memcpy(bottom_, newArray, newSize);
size_ = newSize;
cout << "array has been resized" << endl;
}
int num_items() {
return (top_ - bottom_);
}
char pop(){
top_--;
return *top_;
}
int full() {
return (num_items() >= size_);
}
int empty() {
return (num_items() <= 0);
}
void print(){
cout << "Stack currently holds " << num_items() << " items: ";
for (char *element = bottom_; element<top_; element++) {
cout << " " << *element;
}
cout << "\n";
}
~CStack(){ // stacks when exiting functions
delete[] bottom_;
}
};
int main(){
CStack s(5);
s.print(); cout << "\n";
s.push('s'); s.push('t'); s.push('a'); s.push('c'); s.push('k');
s.print(); cout << "\n";
s.push('=');
s.print(); cout << "\n";
cout << "Popped value is: " << s.pop() << "\n";
s.print(); cout << "\n";
s.push('!');
s.print(); cout << "\n";
s.pop();
s.pop();
s.print(); cout << "\n";
while (!s.empty()) s.pop();
if (s.num_items() != 0) {
cout << "Error: Stack is corrupt!\n";
}
s.print(); cout << "\n";
// destructor for s automatically called
system("pause"); // execute M$-DOS' pause command
return 0;
}
It works fine untill the array is full and i resize it. After that instead of letters it starts printing this . And when the program is done and i have to press any key to exit it gives the following error
Thanks in advance for your help.
You should set top_ to new storage top in resize().
void resize(int newSize){
//Allocate new array and copy in data
char *newArray = new char[newSize];
memcpy(newArray, bottom_, size_);
top_ = newArray + (top_ - bottom_);
//Delete old array
delete[] bottom_;
size_ = newSize;
bottom_ = newArray;
cout << "array has been resized" << endl;
}
The error is indeed in the resize
method:
void resize(int newSize){
//Allocate new array and copy in data
char *newArray = new char[newSize];
memcpy(newArray, bottom_, size_);
// Set the top to the new array
top_ = newArray + (top_ - bottom_);
// Delete old array
delete[] bottom_;
// Update pointers and size
bottom_ = newArray;
size_ = newsize;
cout << "array has been resized" << endl;
}
Your code forgot to fix top_
pointer and used memcpy
to update the pointers when instead simple assignment is needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.