944,056 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 570
  • C++ RSS
Nov 10th, 2009
0

Creating an array of linked lists -- seg fault :'(

Expand Post »
Hello! I'm getting this infamous problem: Segmentation fault (core dumped). Not sure what's going on. ='( Please help! I've red-ed and bolded the parts that I think are problems.

In the following class, I'm trying to create a class called 'BucketHashTable'. I want to create an array of known size (size 52), which contains linked lists. (So I'm trying to create 52 linked lists.) In each of the linked lists, I want to be able to insert an unknown # of objects called 'GrabStuff'. Later, I will want to sort objects inside each individual linked list, but I'm not there yet.

By the way, that class is trying to do a sort-of 'hash' with word inside the 'GrabStuff' object. I'm 'hashing' the first letter of the word, and then putting the corresponding object into appropriate linked list. =| If that makes sense.

Relevant links/files: LinkedList.cpp | LinkedList.h | My entire code so far
Other included files: QuadraticProbing.cpp | QuadraticProbing.h | string.cpp | mystring.h

class BucketHashTable
{
	public:
		BucketHashTable();
		~BucketHashTable();
		void insert(GrabStuff);
	private:
		List <GrabStuff> *bucket[];
};

BucketHashTable::BucketHashTable()
{
	for(int i = 0; i < 52; i++)
		bucket[i] = new List<GrabStuff>();
}

BucketHashTable::~BucketHashTable() {}

void BucketHashTable::insert(GrabStuff g)
{
	int ascii = (int)g.getWord()[0];
	int index = 0;
	if (ascii <= 90)		// if uppercase
		index = ascii%65;
	else 				// if lowercase
		index = ascii%97;
	ListItr<GrabStuff> myIterator = bucket[index]->zeroth();
	bucket[index]->insert(g, myIterator);
}

Thank you very much! I hope it's not too confusing to read. =\ Thanks!
Last edited by crystality; Nov 10th, 2009 at 1:47 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystality is offline Offline
12 posts
since Oct 2009
Nov 10th, 2009
1
Re: Creating an array of linked lists -- seg fault :'(
try :
C++ Syntax (Toggle Plain Text)
  1. private:
  2. List <GrabStuff> *bucket[];

instead of:
C++ Syntax (Toggle Plain Text)
  1. List <GrabStuff> bucket[];
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 10th, 2009
0
Re: Creating an array of linked lists -- seg fault :'(
@ abhi_elementx: Thanks for your reply, but my code *does* use

C++ Syntax (Toggle Plain Text)
  1. private:
  2. List <GrabStuff> *bucket[];

I've tried the other way too. Still doesn't work. =\ Thanks though...
Last edited by crystality; Nov 10th, 2009 at 1:55 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystality is offline Offline
12 posts
since Oct 2009
Nov 10th, 2009
1
Re: Creating an array of linked lists -- seg fault :'(
specify a size while declaring the array:
C++ Syntax (Toggle Plain Text)
  1. List <GrabStuff> *bucket[52];

you can later 'push' elements to the end of the list.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 10th, 2009
0
Re: Creating an array of linked lists -- seg fault :'(
YES! It works! Wow, thank you, abhi_elementx.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystality is offline Offline
12 posts
since Oct 2009
Nov 10th, 2009
1
Re: Creating an array of linked lists -- seg fault :'(
No probs
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Matrix multiplication
Next Thread in C++ Forum Timeline: Linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC