以下程序成功构建了stack
,但是2个操作pop
和stack top
分别给出异常和错误结果。这是程序:
// Working with stack using Array
/*
* @author Suhail Gupta
*
*/
#include <iostream>
using namespace std;
char choice;
int *ptrToArray; // this will be a pointer to the array alias stack that we'll make
int stackSize;
int currentStackSize = 0;
int *firstElement = NULL; // this pointer stores the address of the first element in the stack. It initially points to NULL
void push();
void pop();
void stackTop();
int main() {
cout << "Enter the size of stack : ";
cin >> stackSize;
ptrToArray = new int[stackSize];
do {
push();
cout << "Push elements ? y/n ";
cin >> choice;
} while( choice == 'y');
cout << endl << "pop elements ? y/n ";
cin >> choice;
if( choice == 'y') {
pop();
}
cout << endl << "know the stack top ? y/n ";
cin >> choice;
if( choice == 'y') {
stackTop();
}
}
void push() {
if( currentStackSize == stackSize) { // check before pushing if the stack is full
cout << endl << "Stack-Overflow !";
} else {
int numberToPush;
cout << endl << "Enter the number to be pushed : ";
cin >> numberToPush;
firstElement = &numberToPush; // Store the address of the Last number inserted.This is the address of the first element in the stack
ptrToArray[currentStackSize] = numberToPush;
cout << "The element you just inserted : " << ptrToArray[currentStackSize] << endl;
currentStackSize++;
}
}
void pop() {
if( stackSize == 0 ) {
cout << "Stack Underflow !";
} else {
delete firstElement; // delete the memory allocated to the first element
firstElement = &ptrToArray[currentStackSize-1];
currentStackSize--;
}
}
void stackTop() {
if( firstElement == NULL) {
cout << endl << "Stack Underflow !" << endl;
} else {
cout << "The first element in the stack is : " << *firstElement;
}
}
推送操作正常。但是如果我调用pop
函数,则会显示以下消息:
现在只需评论调用pop
函数的语句。通过调用函数stackTop()
尝试知道堆栈中的第一个元素时得到的结果是The first element in the stack is : -858993460
。这是我在编组garbage number
时没有输入的stack
。为什么语句*firstElement
会给出错误的结果?
答案 0 :(得分:2)
您无法在单个数组元素上调用delete
。一个new[]
必须恰好匹配一个delete[]
。
答案 1 :(得分:2)
int *firstElement = NULL; // Global pointer variable
void push() {
// ...
else {
// ..
int numberToPush; // Resides on stack.
// ....
firstElement = &numberToPush;
// ...
} // numberToPush cease to exist from this point.
}
numberToPush
是一个局部变量,并且else
部分的范围被阻挡,你正在引用它。这会导致垃圾。
编辑:您需要了解全局变量的存储持续时间与本地变量的存储持续时间不同。仅将局部参考引用到全局变量不会增加局部变量的生命周期。一旦它的范围结束,变量就不复存在了。
答案 2 :(得分:1)
容易...
在你的推动中你做了:
firstElement = &numberToPush;
使firstElement获取局部变量的地址,该变量将在函数退出时被删除。 将其更改为:
firstElement = ptrToArray[currentStackSize];
并且您无法删除数组中的单个var 您应该删除Main()
末尾的整个数组只需删除VAR firstElement - 它没用 这样做:
void push() {
if( currentStackSize == stackSize) { // check before pushing if the stack is full
cout << endl << "Stack-Overflow !";
} else {
int numberToPush;
cout << endl << "Enter the number to be pushed : ";
cin >> numberToPush;
ptrToArray[currentStackSize] = numberToPush;
cout << "The element you just inserted : " << ptrToArray[currentStackSize] << endl;
currentStackSize++;
}
}
void pop() {
if( stackSize == 0 ) {
cout << "Stack Underflow !";
} else {
currentStackSize--;
}
}
void stackTop() {
cout << "The first element in the stack is : " << ptrToArray[currentStackSize];
}
答案 3 :(得分:1)
firstElement指向堆栈上的变量:numberToPush。当numberToPush脱离上下文时,firstElement指针变为无效。这就是为什么你的指针在调用删除时崩溃的原因