By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,337 Members | 2,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,337 IT Pros & Developers. It's quick & easy.

garbage in function parameter

P: 1
Hi,

I have designed a linked list with an Iterator to access the nodes of this list in C++. When I call a section of my code that exercises the remove I get garbages in the fuction call parameter. The following is some excerpt of this code.

Expand|Select|Wrap|Line Numbers
  1. class ListIterator
  2. {
  3. public:
  4.     ListIterator( const List* listPtr, ListNode *nodePtr );
  5. private:
  6.     const List* container;
  7.     ListNode* currentNode;
  8. /** Allow access of the private data to the following classes.*/    
  9. friend class List;
  10.  
  11. };//end of ListIterator
  12.  
  13.  
  14. class List {
  15. public:
  16.     List( );
  17.     List( const List& Rhs );
  18. ListIterator remove( ListIterator iter ) throw ( ListException );
  19. private:
  20.     ListNode* head;
  21.     int size;
  22. /** Allow access of the private data to the following classes. */
  23. friend class ListIterator;
  24. }; //end of List
  25.  
  26. class ListNode {
  27. public:
  28.     ListNode( );
  29.     ListNode( const ListItemType&, ListNode* );
  30.  
  31. private:
  32.     ListItemType item;
  33.     ListNode* next;
  34. /** Allow access of the private data to the following classes.*/
  35. friend class List;    
  36. friend class ListIterator; 
  37.  
  38. }; //end of ListNode
  39.  
  40.  
  41. ListIterator
  42. List::remove( ListIterator iter ) throw( ListException )
  43. {
  44.                 /** Check for the correct container. */
  45.     if ( iter.container == this )
  46.     {
  47.         /** Two iterators, prevIter is always behind remvIter.
  48.           */
  49.         ListIterator remvIter = begin( );
  50.         ListIterator prevIter = begin( );
  51.         /** Walk Through the list and find the item to remove. */
  52.         while ( ( remvIter != iter ) && ( (remvIter + 1) != end( ) ) )
  53.         {
  54.             prevIter = remvIter;
  55.             remvIter++;
  56.         }
  57.         /** found the item, remove it and link the nodes.
  58.           * Also, set the iterator to the following node.
  59.           */
  60.         prevIter.currentNode->next = remvIter.currentNode->next;
  61.         prevIter = remvIter + 1;
  62.         size--;
  63.         /** Because of dtor the head is updated.
  64.           * Anytime an object is going out of scope, 
  65.           * each node should be removed and therefore,
  66.           * the head proceeds forward.
  67.           */
  68.         if ( remvIter == begin( ) )
  69.         {
  70.             head = prevIter.currentNode;
  71.         }
  72.         /** guard for memory leak. */
  73.         remvIter.currentNode->next = NULL;
  74.         delete remvIter.currentNode;
  75.  
  76.         /** send an iterator to an item after the removed one. */
  77.         return prevIter;
  78.     }
  79.     else
  80.        throw ListException( "ListException: remove has bad iterator value." );
  81. } //end of remove( )
  82.  
  83. int main( void )
  84. {
  85.          List aList;
  86.          ListIterator anIter = aList.begin( );
  87.  
  88.          for( int j = 1; j <= 2; j++ )
  89.               anIter = aList.insert( anIter, j );
  90.  
  91.         /** Exercise remove method. */
  92.             anIter = aList.begin( );
  93.             anIter++;
  94.             aList.remove( anIter );
  95.  
  96. } //end of main
  97.  
In this code excerpt, I have depsited some integer value into the list and I like to remove the second element from the list.

When aList.remove is called and passed by value, on its return there is some garbage deposited into this anIter. It does not make sense to me. I thought in C or C++ if a function is passed by value, the content of that value of anIter on returned is nont changed. In this case, the content of anIter is garbage.

I like to get your help please.

Thanks,

Reza
Aug 7 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Do you have an implementation of the begin() function?
Aug 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is for a course you are taking, right??

I hope so since there is already a list template in the C++ Standard LIbrary.

The danger of writing a class like this is that after all the work you may want to take it out on the road and use it on a real job. Then all of use are stuck with it forever.
Aug 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.