Thread: Logical error in push node...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    25

    Logical error in push node...

    This is the definition of a method in the class Stack. It is supposed to push a new node in the stack (which is represented as a linked list).Why is this logically wrong although it is error free??

    Code:
    Error_code Stack :: push_stack( Stack_entry item)
    {
    Node new_top(item,top_node);
    top_node=&new_top;
    Return success;
    }
    
    thank you :)
    have a nice day

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    As soon as the function exits, 'new_top' goes out of scope and is destroyed. Use dynamic allocation (don't forget to delete each node when done).

    Code:
    Error_code Stack :: push_stack( Stack_entry item)
    {
    Node * new_top = new Node(item,top_node);
    top_node = new_top;
    Return success;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM