鱼C论坛

 找回密码
 立即注册
查看: 3112|回复: 3

关于赫夫曼编码中的代码有点不理解,请高手多指点!

[复制链接]
发表于 2015-3-4 12:35:23 | 显示全部楼层 |阅读模式
30鱼币
先上代码:(问题在红色字体部分):原码在53课时赫夫曼编码C语言实现  由于代码太多我从上面选了一部分复制下来:

  1. htTree * buildTree(char *inputString)
  2. {
  3.         
  4.         int * probability = (int *)malloc(sizeof(int)*256);
  5.         

  6.         for(int i=0; i<256; i++)
  7.                 probability[i]=0;


  8.         for(i=0; inputString[i]!='\0'; i++)
  9.                 probability[(unsigned char) inputString[i]]++;


  10.         pQueue * huffmanQueue;
  11.         initPQueue(&huffmanQueue);


  12.         for(i=0; i<256; i++)
  13.                 if(probability[i]!=0)
  14.                 {
  15.                         htNode *aux = (htNode *)malloc(sizeof(htNode));
  16.                         aux->left = NULL;
  17.                         aux->right = NULL;
  18.                         aux->symbol = (char) i;
  19.                         
  20.                         addPQueue(&huffmanQueue,aux,probability[i]);
  21.                 }


  22. [color=#ff0000]        [/color][color=#000000]free(probability) [/color][color=#ff0000]; //我调试了程序之后发现这里为什么是每次循环都要释放一次,而不是一次就释放完成!为什么![/color]


  23.         while(huffmanQueue->size!=1)
  24.         {
  25.                 int priority = huffmanQueue->first->priority;
  26.                 priority+=huffmanQueue->first->next->priority;

  27.                 htNode *left = getPQueue(&huffmanQueue);
  28.                 htNode *right = getPQueue(&huffmanQueue);

  29.                 htNode *newNode = (htNode *)malloc(sizeof(htNode));
  30.                 newNode->left = left;
  31.                 newNode->right = right;

  32.                 addPQueue(&huffmanQueue,newNode,priority);
  33.         }


  34.         htTree *tree = (htTree *) malloc(sizeof(htTree));

  35.         tree->root = getPQueue(&huffmanQueue);
  36.         
  37.         return tree;
  38. }

  39. void encode(hlTable *table, char *stringToEncode)
  40. {
  41.         hlNode *traversal;
  42.         
  43.         printf("\nEncoding\nInput string : %s\nEncoded string : \n",stringToEncode);



  44.         for(int i=0; stringToEncode[i]!='\0'; i++)
  45.         {
  46.                 traversal = table->first;
  47.                 while(traversal->symbol != stringToEncode[i])
  48.                         traversal = traversal->next;
  49.                 printf("%s",traversal->code);
  50.         }

  51.         printf("\n");
  52. }
复制代码




最佳答案

查看完整内容

调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所以赶紧释放,免得后边忘记释放导致内存泄漏。 不知道有木有充分理解哥们的问题,如有不对请继续回复。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-3-4 12:35:24 | 显示全部楼层
  1. htTree * buildTree(char *inputString)
  2. {
  3.         //The array in which we calculate the frequency of the symbols
  4.         //Knowing that there are only 256 posibilities of combining 8 bits
  5.         //(256 ASCII characters)
  6.         int * probability = (int *)malloc(sizeof(int)*256);
  7.         
  8.         //We initialize the array
  9.         for(int i=0; i<256; i++)
  10.                 probability[i]=0;

  11.         //We consider the symbol as an array index and we calculate how many times each symbol appears
  12.         for(int i=0; inputString[i]!='\0'; i++)
  13.                 probability[(unsigned char) inputString[i]]++;

  14.         //The queue which will hold the tree nodes
  15.         pQueue * huffmanQueue;
  16.         initPQueue(&huffmanQueue);

  17.         //We create nodes for each symbol in the string
  18.         for(int i=0; i<256; i++)
  19.                 if(probability[i]!=0)
  20.                 {
  21.                         htNode *aux = (htNode *)malloc(sizeof(htNode));
  22.                         aux->left = NULL;
  23.                         aux->right = NULL;
  24.                         aux->symbol = (char) i;
  25.                         
  26.                         addPQueue(&huffmanQueue,aux,probability[i]);
  27.                 }

  28.         //We free the array because we don't need it anymore
  29.         free(probability);

  30.         //We apply the steps described in the article to build the tree
  31.         while(huffmanQueue->size!=1)
  32.         {
  33.                 int priority = huffmanQueue->first->priority;
  34.                 priority+=huffmanQueue->first->next->priority;

  35.                 htNode *left = getPQueue(&huffmanQueue);
  36.                 htNode *right = getPQueue(&huffmanQueue);

  37.                 htNode *newNode = (htNode *)malloc(sizeof(htNode));
  38.                 newNode->left = left;
  39.                 newNode->right = right;

  40.                 addPQueue(&huffmanQueue,newNode,priority);
  41.         }

  42.         //We create the tree
  43.         htTree *tree = (htTree *) malloc(sizeof(htTree));

  44.         tree->root = getPQueue(&huffmanQueue);
  45.         
  46.         return tree;
  47. }
复制代码


调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所以赶紧释放,免得后边忘记释放导致内存泄漏。

不知道有木有充分理解哥们的问题,如有不对请继续回复。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-3-15 20:59:54 | 显示全部楼层
小甲鱼 发表于 2015-3-15 20:42
调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所 ...

哦,我又调试了一下。for 偱环里256次每次都要跑到free(probability)代码上去跑一趟,我以为每跑去一次它都释放了一次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-3-15 21:04:24 | 显示全部楼层
小甲鱼 发表于 2015-3-15 20:42
调用一次 buildTree() 函数只调用一次 free(probability); 噢,写在中间是因为后边已经不需要它了,所 ...

补充一点:
for(i=0; i<256; i++)
                if(probability!=0)
                {
                        htNode *aux = (htNode *)malloc(sizeof(htNode));
                        aux->left = NULL;
                        aux->right = NULL;
                        aux->symbol = (char) i;
                       
                        addPQueue(&huffmanQueue,aux,probability);
                }//如果这里加一个分号的话,就好理解了它就不会每次都跑下边去free()一次了。我试过了加一个分号的话,这个偱环就不会每次跑下去了。而是等for 偱环执行完了才去执行一次free()

        //We free the array because we don't need it anymore
        free(probability);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表