I'm writing a program that given a root of a huffman tree, runs through the tree and gives me back an array of Symbol
s which represents the frequency of each letter in the tree.
Symbol
is defined like this:
typedef struct {
char chr;
int counter;
} Symbol;
And each huffman node is defined like this:
struct HNode;
typedef struct HNode {
char chr;
struct HNode *left, *right;
} HNode;
I don't have an add()
function for a huffman tree, so I made it manually like this:
int main()
{
Symbol * result;
HNode root, left, right, leftleft, leftright, rightleft, rightright, leftleftleft, leftleftright;
root.chr = '\0';
left.chr = '\0';
right.chr = '\0';
leftleft.chr = '\0';
leftleftleft.chr = 'c';
leftleftright.chr = 'd';
leftright.chr = 'e';
rightleft.chr = 'b';
rightright.chr = 'a';
root.left = &left;
root.right = &right;
left.left = &leftleft;
left.right = &leftright;
right.left = &rightleft;
right.right = &rightright;
leftleft.left = &leftleftleft;
leftleft.right = &leftleftright;
leftleftleft.left = NULL;
leftleftleft.right = NULL;
leftleftright.left = NULL;
leftleftright.right = NULL;
leftright.left = NULL;
leftright.right = NULL;
rightleft.left = NULL;
rightleft.right = NULL;
rightright.left = NULL;
rightright.right = NULL;
result = getSL(&root);
while (result->chr != '\0')
{
printf("%c : %d\n", result->chr, result->counter);
result++;
}
getchar();
return 0;
}
The tree looks like this:
The function itself runs recursively through the tree and adds each element to a dynamically allocated array of Symbol
s:
Symbol * getSL(HNode * root)
{
int length;
Symbol * s, *scopy;
length = 0;
s = (Symbol *)calloc((2 + length) * sizeof(Symbol *), 1);
if (!root) return NULL;
if (root->left && root->right)
{
Symbol *s0, *s1;
int s0Length, s1Length;
s = (Symbol *) realloc(s, (2 + length) * sizeof(Symbol *));
s0 = getSL(root->left);
s1 = getSL(root->right);
s0Length = 0;
while ((s0 + s0Length)->chr != '\0')
{
s = (Symbol *)realloc(s, (2 + length) * sizeof(Symbol *));
(s + length)->counter = (s0 + s0Length)->counter + 1;
(s + length)->chr = (s0 + s0Length)->chr;
length++;
s0Length++;
}
s1Length = 0;
while ((s1 + s1Length)->chr != '\0')
{
s = (Symbol *)realloc(s, (2 + length) * sizeof(Symbol *));
(s + length)->counter = (s1 + s1Length)->counter + 1;
(s + length)->chr = (s1 + s1Length)->chr;
length++;
s1Length++;
}
(s + length)->chr = '\0';
}
else
{
s->chr = root->chr;
s->counter = 0;
length++;
(s + length)->chr = '\0';
}
scopy = s;
while (scopy->chr != '\0')
{
printf("%c : %d\n", scopy->chr, scopy->counter);
scopy++;
}
printf("----------------------------------------------\n");
return s;
}
Note: It will be much easier to analyze if you run the program in debug mode as I have added a loop that runs through the array after each phase in the recursion.
The problem comes in this realloc:
s1Length = 0;
while ((s1 + s1Length)->chr != '\0')
{
s = (Symbol *)realloc(s, (2 + length) * sizeof(Symbol *));
(s + length)->counter = (s1 + s1Length)->counter + 1;
(s + length)->chr = (s1 + s1Length)->chr;
length++;
s1Length++;
}
It doesn't happen in one phase, but in the final phase, it does. It says it triggered a breakpoint and if I try to continue, it crashes.
I have absolutely no idea what's wrong and I'll really appreciate your help.
Thank you very much in advance.