1

Im just starting to learn C++ programming and for exercise i found this task. I have to write a dynamic, array based integer stack. This is what i have got so far.

    #include <iostream>

using namespace std;


class DynamicIntegerStack
{
private:
    int *bottom_;
    int *top_;
    int size_;
public:
    DynamicIntegerStack(int n = 20){
        bottom_ = new int[n];
        top_ = bottom_;
        size_ = n;
    }

    int getSize(){ return size_; }

    void push(int c){
        if (!full()){
            *top_ = c;
            top_++;
        }
        else{
            resize(size_ * 2);
            *top_ = c;
            top_++;
        }
    }

    void resize(int newSize){
        //Allocate new array and copy in data
        int *newArray = new int[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;
    }

    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 (int *element = bottom_; element<top_; element++) {
            cout << " " << *element;
        }
        cout << "\n";
    }
    ~DynamicIntegerStack(){ // stacks when exiting functions
        delete[] bottom_;
    }
};
int main(){
    DynamicIntegerStack s(5);
    s.print(); cout << "\n";
    s.push(1); s.push(3); s.push(5); s.push(10); s.push(15);
    s.print(); cout << "\n";
    s.push(20);
    s.print(); cout << "\n";
    cout << "Popped value is: " << s.pop() << "\n";
    s.print(); cout << "\n";
    s.push(30);
    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 integers it starts printing this. DynamicIntegerStack

Thanks in advance for your help.

5
  • 2
    Does it throw a hint your direction if I tell you (a) memcpy uses a byte count for the size argument, and (b) int is very likely larger than a single byte on your platform ? Perhaps try std::copy(bottom_, bottom_+size_, newArray); during the resize and ditch the memcpy. See it live Commented Feb 26, 2015 at 11:47
  • what is your problem ? Commented Feb 26, 2015 at 11:47
  • std::copy gave me errors, it's considered unsafe. Commented Feb 26, 2015 at 11:57
  • BTW, you don't respect the rule of three (five), const are missing, you may use bool, and pop() wrongly returns char. Commented Feb 26, 2015 at 12:04
  • Thank you, i changed pop() method. Commented Feb 26, 2015 at 12:11

1 Answer 1

1

When you use memcpy, the size of the memory you are copying must be given in bytes.

So, you have to multiply sizeof(int) with your n.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.