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

  • SEARCH
  • Home
  • 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 3336186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:06:20+00:00 2026-05-18T00:06:20+00:00

I am currently writing stack that is being implemented with a linked list. I

  • 0

I am currently writing stack that is being implemented with a linked list. I am get this error:

Unhandled exception at 0x75249617 in STACK_LinkedList.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002ee8f8. 

I believe it is possibly coming from either my push() or pop() functions. I can’t find my error. I am fairly new to linked lists, so I have a little bit of a tough time finding errors.

Here is my push() function:

// Adds an item to the top of the stack
template <class S>
void Stack<S>::push(const S & e)
{
    NodePointer temp = new Node(e);

    if ( isEmpty() )
    {
        theTop = theFront = temp;
    }
    else
    {
            // Whatever is after top is stored in temp to keep track of it
        theTop->next = temp;

            // TheTop is stored in temp
        theTop = temp;  
        delete temp; 
    }
}

Here is my pop() function:

//Takes the item off the top of the stack
template <class S>
void Stack<S>::pop()
{
    if ( !isEmpty() )
    {        
                //temp node is set equal to the front
        NodePointer temp = theFront;

                //Holds the second to last node in the linked list
        NodePointer pred;

                //loops through until the node after temp is 0
        while (temp->next != 0)
        {
                        //sets the second to last as temp
            pred = temp ;

                        //basically "increments" temp to the next node
            temp = temp->next ;
        }

                //sets temp equal to the top
        temp = theTop;

                //the top is then set to its predecessor
        theTop = pred;

                //deletes what was known as the top
        delete temp;
    }

    else
        cout << "STACK IS EMPTY" << endl;

}

Thanks alot! I believe most of my logic is correct. I just am missing something small. If it’s something else please tell me and i’ll post that code.

  • 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-18T00:06:21+00:00Added an answer on May 18, 2026 at 12:06 am

    You should not delete your temp in push! It’s a part of the list. So when you access this data later, you get surely an exception.

    Second, you have to initialize your pred with NULL in pop(), otherwise you’ll get an undefined value assigned to theTop if the stack contains only 1 item.

    Third, you should delete in pop() the Node which you allocated in push().

    In general, your approach seems to be not very efficient. You should better store the pointers other way round: from stack top to the bottom items. That way you won’t need to traverse the whole stack on each pop(). Your code will be something like that:

    void push(data)
    {
        allocate new top
        new top's next is the old top
        store new top in the class
    }
    
    void pop()
    {
        if empty, ERROR;
        new top = old top's next
        deallocate old top
    }
    

    Note that you don’t need theFront at all.

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

Sidebar

Related Questions

I am currently writing a simple, timer-based mini app in C# that performs an
I'm currently writing an interface to allow applications to send exception data to a
I'm currently writing a TYPO3 extension which is configured with a list of tt_content
I am currently writing a little bootstrap code for a service that can be
I'm currently writing a bookmarklet that loads and executes a remote js file by
I'm currently writing a client in ActionScript 3 that talks to a Red5 application/media
Im currently writing my bachelor thesis with latex and using TexnicCenter. I want to
Our company is currently writing a GUI automation testing tool for compact framework applications.
I am currently writing a small calendar in ASP.Net C#. Currently to produce the
I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP

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.