473,770 Members | 6,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

garbage in function parameter

1 New Member
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
2 1607
sicarie
4,677 Recognized Expert Moderator Specialist
Do you have an implementation of the begin() function?
Aug 8 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

2
13500
by: Bob | last post by:
Everybody, I've been doing a lot of on-line research and cannot find any reference to the exact problem I'm having. Let me preface this question with the fact that I'm coming from an Oracle background so my approach may not be the best way to tackle this. However, from the research I have done this approach seems reasonable. Also, I know about the undocumented procedure sp_MSforeachtable. That can give me a
34
6434
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do something hardware- related which will get its own driver written in C.
2
2361
by: Daniel Brown | last post by:
I am registering a managed delegate to an unmanaged DLL for asynchronous callback. I understand the garbage collector will free the managed resource if they go out of scope but with the information I have read, it is unclear to me if the garbage collector will move the delegate (e.g. cableInterruptCallbackEventHandler) if it is still referenced thereby invalidating the callback performed by the unmanaged code. I would greatly appreciate...
28
3187
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
0
918
by: batista | last post by:
Hello all, 1) I have a COM compoenent written in vc6.. and im using it in c#...as follows //in C# void getData() { string strData = ""; func( ref strData ); //func() written in vc6....
7
1722
by: Peter Michaux | last post by:
Hi, Below is an example from Flanagan's fifth edition Ajax chapter. It looks to me like the "request" variable might be eligible for garbage collection after this function executes. This would mean the request object is gone by the time the server responds. Or does the closure of the anonymous function make it so that the request object can't be garbage collected?
0
2469
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly over the screen or I drag another window on top of the fast display, I get errors (Pyassertion ....). I believe those are related to a conflict between the thread that is updating the buffer (over which I have control) and the thread that is doing the...
3
4242
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: === def outer():
158
7904
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.