Function pop causing crash

Discussion in 'C++' started by j_depp_99@yahoo.com, Apr 15, 2007.

  1. Guest

    The program below fails on execution and I think the error is in my
    pop function but it all looks correct.Also could someone check my code
    as to why my print function is not
    working? I havent included the other parts of my program but will if
    someone needs it. Please help; I have had it. I have checked all C++
    websites and cannot figure it out.
    <code>

    template<class Type>
    void Novice<Type>::print()
    {
    while(! IsEmpty())
    {
    std::cout << topPtr->item;
    topPtr = topPtr ->next;
    }


    }


    template<class Type>
    void Novice<Type>::pop(Type &y)
    {
    if(IsEmpty())
    throw EmptyStack();
    else
    {
    Node<Type> *tempPtr;
    tempPtr = topPtr;
    topPtr = topPtr -> next;
    delete tempPtr;
    }


    }


    // main function

    int main()
    {
    Novice<int> a;
    int s;

    a.Pop(s); // not working
    a.Push(1);
    a.Push(2);
    a.Push(3);
    cout << a.length() << endl;
    a.Pop(s);
    cout << a.length() << endl;
    a.Print();
    }

    </code>
     
    , Apr 15, 2007
    #1
    1. Advertising

  2. Prashanth Guest

    I guess you are popping from an empty stack and it is throwing an
    exception because IsEmpty will be true. Wrap the code in a
    try{}catch() block and catch the exception. Hope that helps.
     
    Prashanth, Apr 16, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > The program below fails on execution and I think the error is in my
    > pop function but it all looks correct.Also could someone check my code
    > as to why my print function is not
    > working? I havent included the other parts of my program but will if
    > someone needs it. Please help; I have had it. I have checked all C++
    > websites and cannot figure it out.
    > <code>
    >

    If you want some decent help, post something that compiles, otherwise we
    have to guess what a Novice and a Node are.

    --
    Ian Collins.
     
    Ian Collins, Apr 16, 2007
    #3
  4. Ian Collins Guest

    Prashanth wrote:
    > I guess you are popping from an empty stack and it is throwing an
    > exception because IsEmpty will be true. Wrap the code in a
    > try{}catch() block and catch the exception. Hope that helps.
    >

    Please keep the context you are replying to. This post makes no sense
    on its own.

    --
    Ian Collins.
     
    Ian Collins, Apr 16, 2007
    #4
  5. Salt_Peter Guest

    On Apr 15, 4:54 pm, wrote:
    > The program below fails on execution and I think the error is in my
    > pop function but it all looks correct.


    And thats exactly what the program should do - fail. You are
    attempting to pop an empty container and you aren't handling the
    expected and thrown exception. Thats assuming that IsEmpty() is
    working properly.
    Since relying on educated guesses and assumptions is what we are asked
    to disscuss here: try compiling and running the following program for
    illustration.

    #include <iostream>
    #include <stdexcept>

    class EmptyStack : public std::runtime_error
    {
    public:
    EmptyStack()
    : std::runtime_error("EmptyStack error") { }
    };

    int main()
    {
    try {
    throw EmptyStack();
    }
    catch(const std::exception& r_e)
    {
    std::cout << "error: " << r_e.what();
    std::cout << std::endl;
    }
    }

    /*
    error: EmptyStack error
    */

    The std::runtime_error type is derived from std::exception. The thrown
    exception is therefore caught by the above catch block. What should
    the program do if i were to throw EmptyStack() and don't bother
    catching it? Try it.

    int main()
    {
    throw EmptyStack();
    }

    Does the result look familiar to you?
    Note: you don't have to derive from anything insofar as the type
    EmptyStack is concerned. You can throw anything, as long as you use a
    try block and catch it.

    class EmptyStack { };

    int main()
    {
    try {
    throw EmptyStack();
    }
    catch(const EmptyStack& r_e)
    {
    std::cout << "error: EmptyStack" << std::endl;
    }
    // other catch blocks...
    }

    > Also could someone check my code as to why my print function is not
    > working? I havent included the other parts of my program but will if
    > someone needs it. Please help; I have had it. I have checked all C++
    > websites and cannot figure it out.
    > <code>
    >
    > template<class Type>
    > void Novice<Type>::print()
    > {
    > while(! IsEmpty())
    > {
    > std::cout << topPtr->item;
    > topPtr = topPtr ->next;
    > }
    >
    > }


    The function is not 'emptying' the container in any way (IsEmpty() is
    never satisfied). You are therefore stepping through ghost pointers
    (topPtr->next). Anything can happen here - the result is undefined.

    >
    > template<class Type>
    > void Novice<Type>::pop(Type &y)
    > {
    > if(IsEmpty())
    > throw EmptyStack();
    > else
    > {
    > Node<Type> *tempPtr;
    > tempPtr = topPtr;
    > topPtr = topPtr -> next;
    > delete tempPtr;
    > }
    >
    > }
    >
    > // main function
    >
    > int main()
    > {
    > Novice<int> a;
    > int s;
    >
    > a.Pop(s); // not working
    > a.Push(1);
    > a.Push(2);
    > a.Push(3);
    > cout << a.length() << endl;
    > a.Pop(s);
    > cout << a.length() << endl;
    > a.Print();
    >
    > }
    >
    > </code>
     
    Salt_Peter, Apr 16, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad

    ASPX page causing IE to crash

    Brad, May 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,157
  2. John Lynagh via .NET 247

    Authentication mode in web.config causing crash.

    John Lynagh via .NET 247, Jun 10, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    772
    Scott Allen
    Jun 11, 2005
  3. Xiaolei Li
    Replies:
    2
    Views:
    734
    Gordon Beaton
    Apr 26, 2005
  4. Raj Batra
    Replies:
    2
    Views:
    522
    Thomas Heller
    Sep 18, 2003
  5. =?Utf-8?B?TWFuYXNoIFNhaG9v?=

    doPostBack causing a crash in IE7

    =?Utf-8?B?TWFuYXNoIFNhaG9v?=, Feb 26, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,147
    =?Utf-8?B?TWFuYXNoIFNhaG9v?=
    Feb 26, 2007
Loading...

Share This Page