Huffman,Stack overflow

-1
void decodeFile(ibstream& infile, Node* encodingTree, ostream& file) {
    // initializing map here.

    string code = "";
    /*while (true) {
        code += integerToString(infile.readBit());        
        if (map.containsKey(code)) {
            if (map[code] == PSEUDO_EOF) break;
            file.put(map[code]);
            code = "";
        }
    }*/
    bitToString(infile,file,code,map);
}

given method with commented part works fine, I'm tring to do recursion instead of while cycle.

void bitToString(ibstream& infile, ostream& file,string& code,Map<string,ext_char>& map){      
    code += integerToString(infile.readBit());
    if (map.containsKey(code)) {
        if (map[code] == PSEUDO_EOF) return;
        file.put(map[code]);
        code="";
    }
    bitToString(infile,file,code,map);
}

but, with this recursion it gives stack owerflow error on big files.

Unhandled exception at 0x621dffde in Huffman Encoding.exe: 0xC00000FD: Stack overflow.

c++

1 Answer

2

There is a limit of function nesting. If you try for ex. nest function in function 10000 times you will get error. You can use recursion to do Huffman decoding but you need to combine it with iteration (or second recursion). You probably should reset your recursion after finding correct code and iterate to next code until EOF.

answered on Stack Overflow Jun 26, 2016 by Logman

User contributions licensed under CC BY-SA 3.0