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
    Posts
    5,439
    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:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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, 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