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. -
class ListIterator
-
{
-
public:
-
ListIterator( const List* listPtr, ListNode *nodePtr );
-
private:
-
const List* container;
-
ListNode* currentNode;
-
/** Allow access of the private data to the following classes.*/
-
friend class List;
-
-
};//end of ListIterator
-
-
-
class List {
-
public:
-
List( );
-
List( const List& Rhs );
-
ListIterator remove( ListIterator iter ) throw ( ListException );
-
private:
-
ListNode* head;
-
int size;
-
/** Allow access of the private data to the following classes. */
-
friend class ListIterator;
-
}; //end of List
-
-
class ListNode {
-
public:
-
ListNode( );
-
ListNode( const ListItemType&, ListNode* );
-
-
private:
-
ListItemType item;
-
ListNode* next;
-
/** Allow access of the private data to the following classes.*/
-
friend class List;
-
friend class ListIterator;
-
-
}; //end of ListNode
-
-
-
ListIterator
-
List::remove( ListIterator iter ) throw( ListException )
-
{
-
/** Check for the correct container. */
-
if ( iter.container == this )
-
{
-
/** Two iterators, prevIter is always behind remvIter.
-
*/
-
ListIterator remvIter = begin( );
-
ListIterator prevIter = begin( );
-
/** Walk Through the list and find the item to remove. */
-
while ( ( remvIter != iter ) && ( (remvIter + 1) != end( ) ) )
-
{
-
prevIter = remvIter;
-
remvIter++;
-
}
-
/** found the item, remove it and link the nodes.
-
* Also, set the iterator to the following node.
-
*/
-
prevIter.currentNode->next = remvIter.currentNode->next;
-
prevIter = remvIter + 1;
-
size--;
-
/** Because of dtor the head is updated.
-
* Anytime an object is going out of scope,
-
* each node should be removed and therefore,
-
* the head proceeds forward.
-
*/
-
if ( remvIter == begin( ) )
-
{
-
head = prevIter.currentNode;
-
}
-
/** guard for memory leak. */
-
remvIter.currentNode->next = NULL;
-
delete remvIter.currentNode;
-
-
/** send an iterator to an item after the removed one. */
-
return prevIter;
-
}
-
else
-
throw ListException( "ListException: remove has bad iterator value." );
-
} //end of remove( )
-
-
int main( void )
-
{
-
List aList;
-
ListIterator anIter = aList.begin( );
-
-
for( int j = 1; j <= 2; j++ )
-
anIter = aList.insert( anIter, j );
-
-
/** Exercise remove method. */
-
anIter = aList.begin( );
-
anIter++;
-
aList.remove( anIter );
-
-
} //end of main
-
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
2 1607 sicarie 4,677
Recognized Expert Moderator Specialist
Do you have an implementation of the begin() function?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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...
|
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...
|
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....
| |
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?
|
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...
|
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():
|
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
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |