I hope I'm doing this right since it's my first post.

I have a hash table (HList, vector) of lists and I want to use bubble sort to sort my list of entries (HashEntry,class). It compiles fine and temp is a valid list, but my temp.end() is a bad pointer.

void Hash::bubbles()
{
	list<HashEntry> temp;
	list<HashEntry>::iterator end, j, k;
	
	for(int i = 0; i<HList.size();i++)
	{
		temp = HList[i];
		//order the list
		end =  temp.end();  //HERE: bad ptr
		for(int pass = 1; pass<temp.size(); pass++)
		{//j before k
			for(j = temp.begin(), k = temp.begin()++; j != end; j++, k++)
			{
				if((*j)>(*k))
					std::iter_swap(j,k);
			}
			end--;
		}
	}
}

Please help. Thanks.

That's normal. In the STL end() returns a pointer that is 1 past the last element in your container. If it returned a pointer to the actual last element, you would not be able to use it for traversal operations.

This wouldn't work if that wasn't how it behaved:

std::list<int>::iterator liIter
for (liIter = listName.begin(); liIter != listName.end(); ++liIter) {
  //misc. operations
}

@FBody: I don't think thats the problem, because he uses end just like you did in your for loop, for comparisons. And in the line he was pointing to, it definitely shouldn't have a bad pointer exception, if his code were working.

@OP: Can you show us that HList is? I'm guessing its like a Array<list<int>> or something? But more importantly, do you have elements stored at HList for each i?

Thank you both for replying.

@FBody: you are correct.

My bubble sort wasn't doing anything, so I thought it was because of the bad pointer I saw when I debugging. Then I was getting the "list iterator not dereferencable". I worked on it for a while and I searched around see if I can find and answer, but didn't. I posted it and went to sleep.

I woke up, read your post and thought "Oh yea..." and found out the error was elsewhere.

At least now people won't make the same mistake as I have. =D

Be a part of the DaniWeb community

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