Relative Content

Tag Archive for csegmentation-faulthuffman-code

Huffman code segfaults on random input but not on text input

(I asked multiple questions to the implementation of the huffman code here, if I ask too many questions please tell me and I’ll stop. Any tips on how to learn to find those mistakes myself are appreciated, I spent hours and couldn’t find the problem.)

I try to implement huffman code in C. Right now I can successfully read a file, generate the tree and write the compressed data to the output file.
However if my input file is random generated (using something like dd if=/dev/random of=foo bs=100 count=1) the program aborts with a segfault. The weird thing is, this only happens on some random files, some others work.
I’m confident that the null character isn’t the problem because I don’t use any str*() functions (I know printTree() does rely on but I don’t use the function), also one random file that I generated contained an empty byte and it worked. One more time I am profoundly confused as to why my code doesn’t work, more specific why it doesn’t treat all input equally (I would have expected that my code either fails all the time or never as long as the input has the same length).