首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

【哈夫曼树求解】堆被损坏是什么东东,请帮忙分析下

2013-09-05 
【哈夫曼树求解】堆被损坏是什么错误,请帮忙分析下#include stdafx.h#define MAXINT 10000int main(){PHt

【哈夫曼树求解】堆被损坏是什么错误,请帮忙分析下
#include "stdafx.h"
#define MAXINT 10000;
int main(){

PHtTree pht;
int m=10;
int wpl=0;
int i,j;
int m1,m2; //记录最小和次小权值
int x1,x2; //记录最小和次小权值结点的下标
int w[10]={12,32,51,15,63,26,83,22,36,10}; //记录外部结点权值

pht=(PHtTree) malloc(sizeof( PHtTree));
if(pht==NULL)
{
printf("out of space!");
return 1;
}
pht->ht=(PHtNode)malloc(sizeof(PHtNode)*(2*m-1));
if(pht->ht==NULL)
{
printf("out of space!");
return 1;
}

for(i=0;i<2*m-1;i++) //哈夫曼树的初始化
{
pht->ht[i].llink=-1;
pht->ht[i].rlink=-1;
pht->ht[i].parent=-1;

if(i<m)
pht->ht[i].ww=w[i];
else
pht->ht[i].ww=-1;

}

for(i=0;i<m-1;i++)
{

m1=m2=MAXINT;
x1=x2=-1;

for(j=0;j<m+i;j++)

if(pht->ht[j].ww<m1 && pht->ht[j].parent==-1)
{
m2=m1;
x2=x1;
m1=pht->ht[j].ww;
x1=j;
}
else if(pht->ht[j].ww<m2 && pht->ht[j].parent==-1)
{
m2=pht->ht[j].ww;
x2=j;
}

pht->ht[m+i].ww=m1+m2;
pht->ht[x1].parent=m+i;
pht->ht[x2].parent=m+i;
pht->ht[m+i].llink=x1;
pht->ht[m+i].rlink=x2;

}

for(i=m;m<(2*m-1);m++)
{
wpl=wpl+pht->ht[i].ww;
}
for(i=0;i<(2*m-1);i++)
{
printf("%d\n",pht->ht[i].ww);
printf("\n%d\n",wpl);
}
system("pause");
return 0;
}

提示错误:
Windows 已在 huffman.exe 中触发一个断点。

其原因可能是堆被损坏,这说明 huffman.exe 中或它所加载的任何 DLL 中有 Bug。

原因也可能是用户在 huffman.exe 具有焦点时按下了 F12。

输出窗口可能提供了更多诊断信息
[解决办法]


堆是程序员自己管理的,既然你自己是堆破坏了

那就检查所有开辟内存的地方,单步调试

[解决办法]
pht=(PHtTree) malloc(sizeof( PHtTree));
pht->ht=(PHtNode)malloc(sizeof(PHtNode)*(2*m-1));
这使用得都对吗??

malloc返回指针,如struct Test a = (struct Test *)malloc(sizeof(struct Test));
------解决方案--------------------


崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。

#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}

热点排行