堆栈构建程序中的两个问题

时间:2011-09-08 14:41:09

标签: c++ visual-c++ pointers stack

以下程序成功构建了stack,但是2个操作popstack 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函数,则会显示以下消息:

enter image description here

第二个问题:

现在只需评论调用pop函数的语句。通过调用函数stackTop()尝试知道堆栈中的第一个元素时得到的结果是The first element in the stack is : -858993460。这是我在编组garbage number时没有输入的stack。为什么语句*firstElement会给出错误的结果?

4 个答案:

答案 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指针变为无效。这就是为什么你的指针在调用删除时崩溃的原因