This is giving me a segmentation fault in the noBucket() function. I don't see why. Does it having something to do with it being a const function? My other accessor functions
are working fine and they are declared exactly the same with just a different name. The files are longer than this I just pasted the relevant code(from what I could tell).

class Bucket
{
   unsigned short _noBucket;
public:
   unsigned short noBucket() const {return _noBucket;}
};


class Hashtable
{
  Bucket _buckets[30];
public:
  void printReport(ostream& o) {o << "Bucket Number: " << _buckets[i].noBucket() << '\n';}
};

Recommended Answers

Who knows? You certainly have not pasted all the relevant code, because you haven't told us what the value of the variable "i" is or what the noBucket function does.

I will say that your use of "i" makes me suspicious, because it's not a variable in your class. …

Jump to Post

All 3 Replies

Who knows? You certainly have not pasted all the relevant code, because you haven't told us what the value of the variable "i" is or what the noBucket function does.

I will say that your use of "i" makes me suspicious, because it's not a variable in your class. By implication, it's probably a global variable--what else could it be--and any program with a global variable named "i" is cause for suspicion all by itself.

okay here is the full implementation of printReport. I changed the parameters.
GDB says the fault occurs in the noBucket() function when it returns to this function.
noBucket() is simply an accessor for the private member _noBucket which is bucket number.
And this is crashing on the second bucket it reads, so its not out of bounds error. i is within bounds.

void Hashtable::printReport(ostream& o, unsigned short boa)
{
	slot s;
	o << "Hash Table\nVerification Report\n" ;
	if(boa==1)
		o << "Before Restoration\n";
	else
		o << "After Restoration\n\n";
	for(int i = 0; i < 30; ++i)
	{
		o << "Bucket Number: " << _buckets[i].noBucket() << '\n';
		for (int j = 0; j < 3; ++j)
		{
			s = _buckets[i].getSlot(j);
			o << "     Slot Number: " << j << " Key: " << s.Key << " Data: " << s.Data << '\n';
		}
		o << "     Overflow Pointer: " << _buckets[i].getOverflow()->noBucket();
	}
}

as soon as I laid down to sleep I realized what the problem was.....am I the only one? lol...My first bucket doesn't have an overflow pointer because it's not full so it's a null pointer. I was also setting breakpoints in my main and thought I was setting them in my class file which was adding confusion. problem self resolved

class Bucket
{
   unsigned short_noBucket;
   Bucket *_overflow;
public:

   //Returns pointer to bucket overflow.
   const Bucket* getOverflow() const
   {
	return _overflow;
   }  
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.