Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:32+00:00 2026-05-25T11:52:32+00:00

The following program successfully builds up a stack , but the 2 operations pop

  • 0

The following program successfully builds up a stack , but the 2 operations pop and stack top are giving exception and wrong result respectively. Here is the program :

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

The pushing operation works fine.But if i call the pop function the following message is displayed :

enter image description here

Second question :

Now just comment the statement where pop function gets called . The result that i get when trying to know the first element on the stack by calling the function stackTop() is The first element in the stack is : -858993460 . This is some garbage number that i didn’t enter while making up the stack. Why the statement *firstElement gives the wrong result then ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:52:32+00:00Added an answer on May 25, 2026 at 11:52 am
    int *firstElement = NULL; // Global pointer variable
    void push() {
     // ...    
        else {
            // ..
            int numberToPush;  // Resides on stack.
            // ....
            firstElement = &numberToPush; 
            // ...
    
        } // numberToPush cease to exist from this point.
    }
    

    numberToPush is a local variable and have a blocked scope to else part and you are taking the reference of it.This is resulting you the garbage.

    Edit: You need to understand that storage duration of global variables are different to that of local. Just taking the reference of local to a global variable doesn’t increase the life time of a local variable. As soon as it’s scope finishes, the variable ceases to exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following program, DummyMethod always print 5. But if we use the commented
The following program can run successfully: class Simple(object): def __init__(self, name): self.name = name
I am new to Java. I executed the below program successfully but I don't
The following program is very simple: it outputs a single dot each half a
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.