iosfwd exception

This is the code that reads from a txt line by line, and encodes characters using ziv-lempel algorithm. I am not quite sure which operation causes exception. However, I know that exception only occurs when I try to compress large files "with long lines". So large files with medium line length(around 150 characters) work just fine.

exception is as follows:

Unhandled exception at 0x6D9EFC45 (msvcr110d.dll) in compress-program.exe: 0xC0000005: Access violation reading location 0x00000000.


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
  while (getline(read,line))
{
	int length = line.length();

	p = line.substr(0,1);//First character of line

	for (int i = 1; i < length; i++)//Rest of the characters
	{
		c = line.at(i);

		if (compressor.IsExist(p + string(1, c)))
			p = p + string(1, c);
		else
		{
			string word = p + string(1, c);
				
			out << compressor.Encode(p) << " " ;

			if (codeWord < 4096)//If dictionary gets full, just use the existing codes
			{
				compressor.Insert(codeWord, word);
				codeWord++;
			}
			p = string(1, c);
		}
	}
	out << compressor.Encode(p) << "\r\n" ;
}


Following code is where exception occurs in iosfwd lib. :

1
2
3
4
5
6
static int __CLRCALL_OR_CDECL compare(const _Elem *_First1, const _Elem *_First2,
		size_t _Count)
		{	// compare [_First1, _First1 + _Count) with [_First2, ...)
		return (_Count == 0 ? 0
			: _CSTD memcmp(_First1, _First2, _Count));
		}


I thought you might need some other functions code also. So here is the IsExist function's code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool HashTable::IsExist (std::string searchToken) const
{
	int    hash    = Hash(searchToken),
		startPoint = hash;

	do
	{
		if (HashArray[hash].key == searchToken)
			return true;

		hash++;
		hash = hash % 4096;
	} 
	while (HashArray[hash].info != EMPTY && hash != startPoint);

	return false;
}
Last edited on
Hey, thanks for help :P

Anyways, I found the problem. Problem was related to hash table and chars. When hashing characters for hash table, characters with index more than 127 was evaluating to negative integers, thus leading me to call a negative location in array. This was causing a faulty string equality check ( hashTable[negativeIndex] == someString ) hasTable[neg.] was returning NULL since there was no such index.

The solution was to delare chars as unsigned chars or while hashing, changing any negative value to a positive value.
Topic archived. No new replies allowed.