linkedList "first" address changing- don't know why!

Hi, I am to create a set of strings using a linked list, that keeps them in order alphabetically. My main problem comes down to two overloading operators, + and =

The object in question will store a linkedList.
I checked the first address of my list of the returned object, and checked the first address of my list of the parameter's object and they differed! I still cannot figure out why!

Here is the important code related to this problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// strset.h
struct linkedList {
	std::string value;
	linkedList * node;
};

class strSet
{
private:
    linkedList* head;			// This is initially empty (when constructed)		
    bool isSorted () const;

public:
    strSet (); 			  	// Create empty set
    strSet (std::string s); 	 	// Create singleton set
    strSet (const strSet& rtSide);	// Copy constructor
    ~strSet ();			 	// Destructor
...
    strSet  operator +  (const strSet& rtSide);  // Union
    strSet  operator *  (const strSet& rtSide);  // Intersection
    strSet  operator -  (const strSet& rtSide);  // Set subtraction
    strSet& operator =  (const strSet& rtSide);  // Assignment

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 // strset.cpp

...
strSet strSet::operator + (const strSet& rtSide) {
... 
	tempDelete = tempList->head;
	cout << "TempDelete head address: " << tempList->head << endl;

	return *tempList;
}

...
strSet& strSet::operator = (const strSet& rtSide) { 
	strSet *tempList = new strSet;
...
	cout << "rtList head address: " << rtSide.head << endl;
...
}


1
2
3
4
 // main.cpp
...
	tempSet = (tempSet + tempSet2);
...


And this is my output:

TempDelete head address: 0xf810b0
rtList head address: 0xf810f0


If you are wondering, the TempDelete's next node address was 0xf810d0

I have pulled my hair out wondering why and how this happened but I'll continue testing this out. Thanks for all the help!


Last edited on
You are returning a copy, not a pointer to the data, is why. Look at line 4 and 9 from your source snippet. Your return value is not a pointer and you are returning the pointer dereferenced - a copy of the data the pointer is pointing to.
Hmm.. alright I suppose that makes sense. I was just thinking returning the pointer dereferenced would still retain the original address when I send it through the other parameter.

The main issue I am having with this program is that I loop through the linkedList until I hit a NULL node. When I check through tempSet, this works successfully.

I use a while loop iterating through: tempDelete = tempDelete->node; and when tempDelete == NULL I terminate.

However in my assignment operator, my rtList never has a NULL node! I tried figuring out why by printing out rtLists' addresses and I noticed that rtList's head node had a different address than tempSet.

However to answer my main issue, rtSide.head points to a never ending linkedList.

For example I have:
strSet tempSet;
strSet tempSet2( "a" ) <--- a string with value "a" ;
1
2
3
4
//main.cpp
...
tempSet = (tempSet + tempSet2);
...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// strSet.cpp
...
strSet& strSet::operator = (const strSet& rtSide) { 
	nullify();
	linkedList *newList, *rtList = rtSide.head;
	int count = 0;
	if( rtSide.head == NULL ) { 
	        head = NULL; 
		return *this;
	}
	cout << "rtList head address: " << rtSide.head << endl;
	head = new linkedList;
	newList = head;
	do { 
		newList->node = new linkedList;
		newList->value = rtList->value;

		rtList         = rtList->node;
		cout << "rtList address: " << rtList << endl;
		cout << "rtList value : " << rtList->value << endl;
		if(rtList == NULL) cout << "RT LIST HAS A NULL HEAD!" << endl;
		newList        = newList->node;
		count ++;
	}while( rtList != NULL  && count < 10);
	cout << " Yout got here!" << endl;

	delete newList;
	newList = NULL;
	cout << "You got here!2" << endl;
	return *this;
}


The output for this is
 
TempDelete head address: 0xf810b0
rtSide head address: 0xf810f0
rtList address: 0xf81110
rtList value : 
rtList address: 0xf81130
rtList value: a 
rtList address: 0xf81150
rtList value: 
rtList address: 0xf81170
rtList value: a 
rtList address: 0xf81190
rtList value: 
...
you got here!
you got here!2


I understand that dereferencing it has made a proper copy of that object, yet why does it's linked list go on forever? The original tempSet has a finite linked List.

EDIT: To further prove what I mean, this is what happens when I iterate through tempList:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 //strset.cpp
...
strSet strSet::operator + (const strSet& rtSide) {
...
	tempDelete = tempList->head;
	cout << "TempDelete head address: " << tempList->head << endl;
	while(tempDelete != NULL) {
		cout << "tempDelete address: " << tempDelete << endl;
		cout << "tempDelete node   : " << tempDelete->node << endl;
		tempDelete = tempDelete->node;
	}

	return *tempList;
...

This is the output I get with tempList:

TempDelete head address: 0x8d00b0
tempDelete address: 0x8d00b0
tempDelete node   : 0x8d00d0
tempDelete address: 0x8d00d0
tempDelete node   : 0
Last edited on
Topic archived. No new replies allowed.