Thread: Destructor crash

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Destructor crash

    Hey guys,

    I have been learning c++ while working with memory management i seem to have gotten stuck,
    There are 2 classes Stack and Queue the stack works on the LIFO principle (last in first out) and the queue the opposite FIFO (first in first out).

    Now the program runs fine however when the queue destructor gets called the program crashes trying to free the memory,
    I cant figure out why this happens.

    Any help is would be appreciated!

    Abby

    Code:
    #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;
    	}
    	void push(char c){
    		if (num_items() >= size_)
    		{
    			size_ += 1;
    			realloc(bottom_, size_);
    		}
    		*top_ = c;
    		top_++;
    	}
    	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(){
    		delete [] bottom_;
    	}
    };
    
    class CQueue
    {
    private:
    	char *bottom_;
    	char *content_;
    	char *top_;
    	int size_;
    public:
    	CQueue(int n =20){
    		content_ = new char[n];
    		bottom_ = content_;
    		top_ = bottom_;
    		size_ = n;
    	}
    	void push(char c){
    		*top_ = c;
    		top_++;
    	}
    	int num_items() {
    		return (top_ - bottom_ );
    	}
    	char pop(){
    		char test = *bottom_;
    		bottom_++;
    		return test;
    	}
    	int full() {
    		return (num_items() >= size_);
    	}
    	int empty() {
    		return (num_items() <= 0);
    	}
    
    	void print(){
    		cout << "Queue currently holds " << num_items() << " items: " ;
    		for (char *element=bottom_; element<top_; element++) {
    			cout << " " << *element;
    		}
    		cout << "\n";
    	}
    
    	~CQueue(){ 
    		delete [] content_;
    	}
    };
    
    
    int main(void)
    {
    	CStack s(5);
    	CQueue q(5);
    
    	q.print(); cout << "\n";
    	q.push( 's');q.push('t');q.push('a');q.push('c');q.push('k');
    	q.print(); cout << "\n";
    
    
    	if (!q.full()) q.push('='); 
    
    	q.print(); cout << "\n";
    	cout << "Popped value is: " << q.pop() << "\n";
    	cout << "Popped value is: " << q.pop() << "\n";
    	q.print(); cout << "\n";
    	q.push('!');
    	q.print(); cout << "\n";
    	q.pop();
    	q.pop();
    	q.print(); cout << "\n";
    	while (!q.empty()) q.pop();
    	if (q.num_items() != 0) {
    		cout << "Error: Stack is corrupt!\n";
    	}
    	q.print(); cout << "\n";
    	
    	s.print(); cout << "\n";
    	s.push( 's');s.push('t');s.push('a');s.push('c');s.push('k');
    	s.print(); cout << "\n";
    
    
    	if (!s.full()) 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";
    	
    	system ("pause");
    	return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I would check out your push function in CStack.

    If you are allocating memory with new, you cannot use the C memory functions(free, realloc, etc).

    You will need to write your own reallocation function.
    Woop?

Popular pages Recent additions subscribe to a feed