Dynamic memory issues

I'm working on a dynamic stack, and I keep getting a heap corruption error when expandStack() is called.
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
const int INIT_STACK = 200;
class stack {
private:
	char* data;
	int top;
	void expandStack();
public:
	stack();
	~stack();
	bool push(char x);
	char pop();
	bool isEmpty();
};
stack::stack()
{
	top = -1;
	data = new char[INIT_STACK];
}
stack::~stack()
{
	delete[ ] data;
}
void stack::expandStack()
{
	char* newdata = new char[top+INIT_STACK+1];
	for(int i=0;i<top+1;i++)
	{
		newdata[i] = data[i];
	}
	delete [ ]data; // this line causes the error
	data = newdata;
}
bool stack::push(char x)
{
	if (top == INIT_STACK)
		expandStack();
	data[++top] = x;
	return true;

}
char stack::pop()
{
	if (top == -1)
		return 'z';
	return data[++top];
}
bool stack::isEmpty()
{
	if (top == -1)
		return true;
	return false;
}

main looks something like this
1
2
3
4
5
6
7
8
9
10
11
12
	stack myStack;
	myStack.push('s');
	for(int i=0;i<198;i++)
	{
		myStack.push('t');
	}
	myStack.push('t');
	myStack.push('t');
	myStack.push('t');
	myStack.push('t');
	myStack.push('t');
	myStack.push('x');
Last edited on
What's the value of MAXSTACK?

EDIT: If it's bigger than INIT_STACK you have heap corruption, caused by data[++top] = x;
Last edited on
sorry, that's a typo. MAXSTACK should be INIT_STACK
Well, it works ok for me... No problem at all. I'll take a closer look and tell you if I find anything. By the way, your pop function should look like this:

1
2
3
4
5
6
char stack::pop()
{
    if (top == -1)
        return 'z';
    return data[top--];
}

But that's irrelevant to the problem...

EDIT: Also, your push function should look like this:

1
2
3
4
5
6
7
8
bool stack::push(char x)
{
    if (top == INIT_STACK-1)
        expandStack();
    
    data[++top] = x;
    return true;
}

You see, in your version, when top==INIT_STACK-1 expandStack() won't be called and data[++top]=x; is equivalent to top++; data[INIT_STACK]=x; which can also cause heap corruption.
Last edited on
Awesome. Thanks for your help!
For anyone interested, the final version looks like this:
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
const int INIT_STACK = 200;
class stack {
private:
	char* data;
	int top;
	void expandStack();
public:
	stack();
	~stack();
	bool push(char x);
	char pop();
	bool isEmpty();
};
stack::stack()
{
	top = -1;
	data = new char[INIT_STACK];
}
stack::~stack()
{
	delete[ ] data;
}
void stack::expandStack()
{
	char* newdata = new char[top+INIT_STACK+1];
	for(int i=0;i<top+1;i++)
		newdata[i] = data[i];
	delete[ ] data;
	data = newdata;
}
bool stack::push(char x)
{
    if (top % INIT_STACK-1 == 0)
        expandStack();
    data[++top] = x;
    return true;
}
char stack::pop()
{
    if (top == -1)
        return 'z';
    return data[top--];
}
bool stack::isEmpty()
{
	if (top == -1)
		return true;
	return false;
}
Last edited on
Topic archived. No new replies allowed.