霍夫曼树触发断点

时间:2014-07-06 14:31:32

标签: c breakpoints huffman-code

我正在编写一个程序,该程序给出了一个霍夫曼树的根,穿过树并给我一个Symbol的数组,它代表了树中每个字母的频率。
Symbol的定义如下:

typedef struct {
    char chr;
    int counter;
} Symbol;

每个霍夫曼节点的定义如下:

struct HNode;
typedef struct HNode {
    char chr;
    struct HNode *left, *right;
} HNode;

我没有霍夫曼树的add()功能,所以我这样手动制作:

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

树看起来像这样:My huffman tree

函数本身以递归方式在树中运行,并将每个元素添加到动态分配的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;
}

注意:如果你在调试模式下运行程序会更容易分析,因为我已经在递归的每个阶段之后添加了一个遍历数组的循环。

问题出现在这个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++;
        }

它不会发生在一个阶段,但在最后阶段,确实如此。 它说它触发了一个断点,如果我试图继续,它会崩溃。

我完全不知道出了什么问题,我非常感谢你的帮助。

非常感谢你。

1 个答案:

答案 0 :(得分:1)

您没有为s分配足够的内存。您似乎希望s指向Symbol的数组,但您只为Symbol*数组分配空间:

s = (Symbol *)realloc(s, (2 + length) * sizeof(Symbol *));

这为多个指针分配了足够的空间,但是realloc的结果被用作指向几个Symbol结构的空间:

(s + length)->counter = (s1 + s1Length)->counter + 1;

如果sizeof(Symbol)> sizeof(Symbol*)这意味着您可能会写过分配的空间,破坏内存。

您可能希望在分配中使用sizeof(Symbol)

s = (Symbol *)realloc(s, (2 + length) * sizeof(Symbol));

(此外,投放calloc / realloc的返回值是不必要的,我不确定为什么要分配2+length元素 - 1+length似乎是足够了。)