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.
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. :
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.