Monday, 15 February 2010

c++ - Weird result on simple linked list code -


OK I'm paying around with a simple linked list code.

If I keep the head node in the public, then I declare an indicator (head 2) to store the top node of the first list (first) in the main program. I declare second name as second list, and assign head 2 as the top node of the second list. Then I remove head2 then I reach out to the members of "seconds" (whose head has been removed from the node) and print them I hope to make a split mistake, but it works, only the data of the head node For printing is 0. What is for me, is that if the head node has been removed, then what is the next indicator of the head node in memory? (It is accessed by print to sort through the list. I am using G ++ 4.6.1 in Ubuntu. Here is the code:

  # include  data = one; navy-> next = head; head = nawness;} zero list 1 :: print () {n} Od * dummy = head; while (dummy) {std :: cout  data  gt; next;}} int Main () {list1 first; first.insert (1); first.insert (2); first.insert (4); first.insert (9); list 1 second; node * head2 = new node; head2 = first Head; second head = head 2; delete head2; second print (); return 0;}    

Your code invokes undefined behavior by accessing the object whose lifetime has expired.

There is nothing weird to do that it can still work.

Undefined behavior can mean anything happiness, in which you are looking.

No comments:

Post a Comment