Huffman problem -> Floating Point Exception

Hello, I'm working on a Huffman Coding program and I'm having some trouble with my decompression. I'm getting a Floating Point Exception error when I try to decompress a file and I don't know where the problem lies. Here is my function:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
void HuffmanCoding::decompress(string input, string output)
{
    //initalize variables
    codeTree = new mergingTree<mergingType>;
    inFile = input;
    outFile = output;
    fileBuffer = new int[1000];
    fileBuffer2 = new int[1000];
    counts = new int[256];
    fileBufferSize = fileBufferSize2 = bufferSize = 0;
    readChars = 0;
    readDone = false;

    for(int i = 0; i < 256; i++)
        counts[i] = 0;

    //read file's signature
    cout << "Reading file's signature..." << flush;
    readSignature();

    //Create output file
    ofstream outputfile(outFile.c_str(),ios::out | ios::binary);

    if(!outputfile.is_open())
        throw runtime_error( "Cannot create the output file!");

    outputfile.close();

    //insert nonzero counts into mergingTree's heap
    // and set other variables
    cout << "Done\nSetting necessary variables for decompressing..." << flush;

    int nonzeros = 0;
    for(int i = 0; i < 256; i++)
    {
        if(counts[i] != 0)
        {
            nonzeros++;
            mergingType tmp(i,counts[i]);
            codeTree->insertNode(tmp);
        }
    }

    //get file's size and precent to show the progress
    unsigned long inputfilesize = fileSize(),
        currpos = 0, percent, outputfilesize = 0;
    inputfilesize -= ( (nonzeros * 8) + 5 );
    percent = inputfilesize / 100;
    int curper = 0;

    //create mergingTree's tree
    // and also get a copy of mergingTree's root
    codeTree->createTree();
    codeTree->createHuffmanArrays();
    mergingTreeNode< mergingType > *rootCopy, *tmp;
    rootCopy = codeTree->getRootCopy();
    tmp = rootCopy;

    //each time the file's buffer is empty
    // these variables are hold to keep track
    // of the last operation and continue it's job
    int tmpj =0,tmpk=0;
    bool middle = false;

    cout << "Done\nDecoding file...\n0 % (0) Bytes." << flush;

    //if we reach to the end of file
    // the readDone variable sets to true
    while(!readDone)
    {
        readFile(); //read 1000 bytes to file's buffer
        bool done = false; //to check if we reach to one leaf or not
        int i = 0,j = 0,k = 0; //counters

        //go through file's buffer
        for(i = 0; i < fileBufferSize2; i++, currpos++)
        {
            //check each bit
            for(j = 0, k = 128; j < 8; j++, k /= 2)
            {
                //check if we continue track of the last
                // character of the file's buffer
                if(middle)
                { //so restore the variables
                    middle = false;
                    j = tmpj;
                    k = tmpk;
                }

                //if all bits are done and the rest is
                // the zeros we filled with zero
                // so break and do not continue
                if( (readDone && (zeros == (9 - j))) &&
                    ( i == (fileBufferSize2 - 1)) )
                {
                    done = true;
                    break;
                }

                done = false;

                //if current bit is 0
                if( (fileBuffer2[i] & k) == 0 )
                {
                    //if this is a leaf
                    if( tmp->leftPtr == NULL )
                    {
                        done = true;
                        //if file's buffer is full so write it to file
                        if(fileBufferSize == 1000)
                            writeTheFile();
                        //write character to buffer
                        fileBuffer[fileBufferSize++] = (tmp->data).data;
                        outputfilesize++;
                        tmp = rootCopy->leftPtr;
                    }
                    else //go to left (for zeros we go to left)
                        tmp = tmp->leftPtr;
                }
                else
                {
                    //if this is a leaf
                    if( tmp->rightPtr == NULL )
                    {
                        done = true; //for leaves
                        //if file's buffer is full so write it to file
                        if(fileBufferSize == 1000)
                            writeTheFile();
                        //write character to buffer
                        fileBuffer[fileBufferSize++] = (tmp->data).data;
                       outputfilesize++;
                        tmp = rootCopy ->rightPtr;//go to right
                    }
                    else //go to right (for zeros we go to right)
                        tmp = tmp->rightPtr;
                }
            }

            //if done one percent
            if( (currpos % percent) ==  0 )
            {
                cout << '\r' << ++curper << " % ("
                    << currpos << ") Bytes.";
            }
        }
        //we didn't write the last character
        if( (zeros == 0) && readDone )
            fileBuffer[fileBufferSize++] = (tmp->data).data;

        if(done) //nothing to continue
        {
            fileBufferSize2 = 0; //reset file's buffer size
            middle = false; //no need
        }
        else
        {
            if(j != 8) //if we're at the middle of character
            {
                //backup variables
                middle = true;
                tmpj = j - 1;
                tmpk = k * 2;
                fileBuffer2[0] = fileBuffer2[fileBufferSize2];
                fileBufferSize2 = 1;
            }
            else //we've done all the bits
            {
                fileBufferSize2 = 0; //reset file's buffer size
                middle = false; //no need
            }
        }
    }
    //show the 100% progress
    if(fileBufferSize != 0)
        writeTheFile();
    cout << '\r' << "100 % ("
        << inputfilesize << ") Bytes.";
    cout << "\nOutpuf file size: " << outputfilesize;
    cout << "\nCompression operation completed successfully.";

    //release dynamic memory
    cout << "\nReleasing memory..." << flush;

    delete [] fileBuffer2;
    delete [] fileBuffer;
    delete [] counts;
    delete codeTree;
    cout << "Done";

}


The error happens right after line 65: cout << "Done\nDecoding file...\n0 % (0) Bytes." << flush;

Thanks for your time.
Last edited on
Have you tried running it through a debugger?
I get this from the debugger:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Building to ensure sources are up-to-date
Build succeeded
Selecting target: 
Release
Adding source dir: C:\Users\Jacob\Desktop\Huffman\Huffman\
Adding source dir: C:\Users\Jacob\Desktop\Huffman\Huffman\
Adding file: bin\Release\Huffman.exe
Starting debugger: 
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
(no debugging symbols found)
Debugger name and version: GNU gdb 6.7.50.20071127
Child process PID: 3812
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
Program received signal SIGFPE, Arithmetic exception.
In ?? () ()
Debugger finished with status 0
Topic archived. No new replies allowed.