Logical error in push node...

This is a discussion on Logical error in push node... within the C++ Programming forums, part of the General Programming Boards category; This is the definition of a method in the class Stack. It is supposed to push a new node in ...

  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,643
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

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, 03: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21