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

#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;
}

That code compiles/runs fine on gcc 4.3.3. No crash/segfault.

1. Wrong reallocation in CStack::push. You must assign new memory block pointer value to the bottom_ pointer (see realloc specification carefully). Now you have memory corruption after realloc call. However it's wrong method to reallocate memory obtained by operator new with C library function realloc. Avoid using malloc, calloc and realloc in C++ programs.
2. You don't check up CQueue buffer overflow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.