哈夫曼,堆栈溢出

时间:2016-06-26 14:39:53

标签: c++

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);
}

给定的方法与评论部分工作正常,我想做递归而不是循环。

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);
}

但是,通过这种递归,它会在大文件上产生堆栈功率错误。

  

Huffman Encoding.exe中0x621dffde处的未处理异常:0xC00000FD:   堆栈溢出。

1 个答案:

答案 0 :(得分:2)

功能嵌套存在限制。如果你尝试前。函数嵌套函数10000次就会出错。您可以使用递归来执行霍夫曼解码,但您需要将其与迭代(或第二次递归)结合使用。您可能应该在找到正确的代码后重置递归并迭代到下一个代码直到EOF。

相关问题