Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a run time error when I instantiate an object in a PhoneBook class which is an array of doubly linked lists. The code is shown below and the error one line after a comment which says ERROR BELOW. I would appreciate any help. This is just a personal exercise I am doing that I obtained from the internet. Thank you.

C++
#include <iostream>
#include <stdlib.h>

using namespace std;

// Person: name and phone number
class Person
{

public:
	// initialize name and phone number, assuming phone is PHONELEN digits
	//constructor define the Person object
	Person(const char name[], const char phone[])
	{
		// phone is 10 characters plus \0
		// person is 15 characters plus \0

		if (strlen(name) <= NAMELEN) { strcpy(this->name, name); }
		else { cout << "name was greater than size limit of 15 char" << endl; }

		if (strlen(phone) <= PHONELEN) { strcpy(this->phone, phone); }
		else { cout << "name was greater than size limit of 10 char" << endl; }
	}

	// return true if OTHER's phone number is the same as this one
	bool samePhone(const Person *other) const
	{
		if (strcmp(this->name, other->getName()) == 0) { return true; } // same name
		else { return false; }
	}

	// return hash value for object as an integer between 0 and 99 (inclusive)
	int Person::hashValue() const
	{
		char cDig1, cDig2;
		cDig1 = phone[8];
		cDig2 = phone[9];

		int dig1, dig2, result;
		dig1 = int(cDig1 - '0');
		dig2 = int(cDig2 - '0');
		result = 10*dig1 + dig2;

		return result%101; 
	}

	// return name field
	const char* getName() const
	{
		return name;
	}

	enum { NAMELEN = 81, PHONELEN = 10 };
protected:
	char name[NAMELEN]; // null-terminated string
	char phone[PHONELEN]; // *not* null-terminated
};

// PersonList: a linked list of Person objects
class PersonList
{
private:
	static int count;
	char name[Person::NAMELEN]; // null-terminated string
	char * name2; 
	char phone[Person::PHONELEN]; // *not* null-terminated

	Person * aPerson;

	struct PersonNode
	{
		Person *person;
		PersonNode *prev, *next;
		PersonNode::PersonNode(Person *pers, PersonNode *n, PersonNode *p)
			: person(pers), next(n), prev(p)
		{ }
	} *front, *back;
	PersonList(const PersonList&) // hide copy constructor
	{}

	void operator=(const PersonList&) // hide assignment operator
	{}

	bool empty() const { return ( !front || !back ); } // quick inline function to determine if list is empty

public:
	// initialize list to be empty
	PersonList()
		:front(NULL), back(NULL)
	{
		char * tempName = "JoeSmith";
		char * tempPhone = "1234561234";
		strcpy(name, tempName);
		strcpy(phone, tempPhone);
		aPerson = new Person(name, phone);
	}

	// remove all entries from the list, deleting the entries as well
	~PersonList()
	{
		while(front)
		{
			PersonNode * temp = front;
			front = front->next;
			delete temp;
		}
	}

	// add: add NEW_PERSON to front of list; assumes NEW_PERSON not already
	// in the list
	void add(Person *new_person)
	{
		// ERROR BELOW, occurs on next line front = 
		front = new PersonNode(new_person, front , NULL); //previous is NULL, next is current front
		count++;
		if(front->next)
		{
			front->next->prev = front;
		}

		if ( empty() )
		{
			back = front;
		}
	}


	// lookup: return object in list with same phone number as TARGET;
	// also moves the found object to the front of the list
	Person *lookup(const Person *target)
	{
		PersonNode * current = front;
		while(current)
		{
			if(current->person->samePhone(target))
			{
				if (current==front)
				{
					return current->person;
				}
				else if (current == back)
				{
					PersonNode * objectToMove = current;
					back = current->prev; //#1
					current->prev->next = NULL; //#2
					front->prev = objectToMove; //#3
					objectToMove->prev = NULL; //#4
					objectToMove->next = front; //#5
					front = objectToMove; //#6
					return objectToMove->person;
				}
				else
				{
					PersonNode * objectToMove = current;
					current->prev->next = current->next; //#1 First take care of items
					current->next->prev = current->prev; //#2
					front->prev = objectToMove; //#3
					objectToMove->prev = NULL; //#4
					objectToMove->next = front; //#5
					front = objectToMove; //#6
					return objectToMove->person;
				}
			} // end current->person->samePhone(target) 
			current = current->next;
		} // end of while
		return NULL;
	} // end of lookup

	// remove: remove TARGET from the list; does nothing if there is no pointer
	// in the list equal to TARGET
	void remove(const Person *target)
	{
		PersonNode * current = front;

		while(current)
		{ //if(current->person->samePhone(target))
			if(current->person == target )
			{
				if (current==front)
				{
					current->next->prev = NULL;
					front = current->next;
					//cout << current->person->getName() << " with number " << current->
					delete current;
					count--;
					break;
				}
				else if (current == back)
				{
					current->prev->next = NULL;
					back = current->prev;
					delete current;
					count--;
					break;
				}
				else
				{
					current->prev->next = current->next;
					current->next->prev = current->prev;
					delete current;
					count--;
					break;
				}
			} //end if current==front
			current = current->next;
		} // end while
	} // end of remove

	// length: return number of entries on list
	int length() const
	{
		return count;
	}
};

int PersonList::count = 0;

class RevPhoneBook
{
private:
	static int count;
	PersonList * personListPtr[101];
	PersonList oneList;
public:
	RevPhoneBook()
	{}

	~RevPhoneBook()
	{
	} 

	void add(Person* new_person)
	{
		int locationOfPerson = new_person->hashValue();
		count++;
		personListPtr[locationOfPerson]->add(new_person);
	}

	Person* lookup(const Person* target)
	{
		for (int j=0; j<count; ++j) {
			if(personListPtr[j]->lookup(target))
			{
				return personListPtr[j]->lookup(target);
			}
		}
	}

	void remove(const Person* target)
	{
		for (int j=0; j<count; ++j) {
			if(personListPtr[j]->lookup(target) == target)
			{
				personListPtr[j]->remove(target);
			}
		}
	}
};
int RevPhoneBook::count = 0;


int main()
{
	Person adrian("adrian", "9565420346");
	Person carlos("carlos", "7171234567");
	Person tom("tom", "9721234567");
	Person jack("jack", "6821234567");
	Person miguel("miguel", "9561234567");

	RevPhoneBook myPhoneBook;
	myPhoneBook.add(&adrian); //RUN TIME ERROR OCCURS HERE!!
	// LOOK AT NOTES ABOVE in add

	return 1;
}
Posted
Updated 19-Sep-12 5:32am
v3
Comments
[no name] 19-Sep-12 11:25am    
So you got some random bit of code that did not work and you dump it here wanting someone to debug it for you?
pasztorpisti 19-Sep-12 11:33am    
Looking at the code I'm not surprised about the runtime error...
Victor Arnoldo Olvera 19-Sep-12 12:39pm    
I am using an interface from an Advanced Data Structures course I found on the internet. I'm trying to use the class definition of PersonNode as is, but maybe I need to add more to it or change it somehow so it will work. I feel I have a chicken and egg problem. Do you have any ideas? Thank you.

1 solution

C++
void add(Person* new_person)
{
  int locationOfPerson = new_person->hashValue();
  count++;
  personListPtr[locationOfPerson]->add(new_person); //<-- HERE 
}


At the marked line, personListPtr[locationOfPerson] does NOT point to a valid memory buffer.
 
Share this answer
 
Comments
Chuck O'Toole 19-Sep-12 15:30pm    
Yup, the pointer list is uninitialized. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900