主题:[求助]asertion failure
pjfwarlock
[专家分:0] 发布于 2006-12-28 12:02:00
void CreatHuffmanTree(HuffmanTree &HT, int* w, int n){
if( n <= 1 )
return;
int m = 2 * n - 1;
HT.HTree = new HTNode[m];
HTNode *p;
int * fla = (int *)malloc(m * sizeof(int));
int i, s1, s2;
for(i = 0; i < m; ++i)
fla[i] = 0;
for(p = HT.HTree, i = 0; i < n; ++i){
p[i].weight = w[i];
p[i].lchild = -1;
p[i].rchild = -1;
}
for(;i <= m; ++i){
p[i].weight = 0;
p[i].lchild = -1;
p[i].rchild = -1;
}
for(i = n; i < m;++i){
Select(HT.HTree, i - 1, s1, s2,fla);
HT.HTree[i].lchild = s1;
HT.HTree[i].rchild = s2;
HT.HTree[i].weight = HT.HTree[s1].weight + HT.HTree[s2].weight;
}//for
HT.root = m - 1;
[color=FF0000]//free(fla);[/color[color=0000FF]]//这句为什么不能有,一有就是assertion failure[/color] //delete [] fla;
}//CreatHuffmanTree
void Select (HTNode *HTree , int size , int &s1 , int &s2 , int * &flag)
{
int i,j,k,khelp;
for ( j = 0,k = 0, khelp = 0; j < 2 ; j++)
{
for ( i = 0 ; i <= size ; i++)
{
if (khelp == 0)
{
if (flag[i] == 0)
{
k = i;
khelp = 1;
}
else
continue;
}
else
if (flag[i] == 0 && HTree[i].weight < HTree[k].weight)
k = i;
}
if (j == 0)
s1 = k;
else
s2 = k;
flag[k] = 1;
k = 0;
khelp = 0;
}
}
回复列表 (共9个回复)
沙发
雪光风剑 [专家分:27190] 发布于 2006-12-28 12:20:00
是不是一次释放连续指针不被允许?
板凳
pjfwarlock [专家分:0] 发布于 2006-12-28 12:22:00
没有啊,其他地方我者是这样用的,而且用C++的new和delete也不行!
3 楼
pjfwarlock [专家分:0] 发布于 2006-12-28 12:24:00
完整代码:这是读一个文本文件,进行Huffman编码的程序
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
LNode *next;
}LNode, *LinkList;
void InitList_L(LinkList *L)
{
L = NULL;
}
int ListLength_L(LinkList L)
{
LNode *p;
int k = 0;
p = L;
while(p)
{
k++;
p = p->next;
}
return k;
}
LNode * LocateElem_L(LinkList L,ElemType e)
{
LNode *p;
p = L;
while(p && p->data != e)
p = p->next;
return p;
}
void ListDelete_L(LinkList &L,LNode *p, ElemType &e)
{
LNode *q;
if(p == L)
{
L = p->next;
}
else
{
q = L;
while(q->next != p)
q = q->next;
q->next = p->next;
}
e = p->data;
delete p;
}
void DestroyList_L(LinkList &L)
{
LNode *p,*pre_p;
p = L;
while(p->next != NULL)
{
pre_p = p;
p = p->next;
delete pre_p;
}
delete p;
L = NULL;
}
void ClearList_L(LinkList &L)
{
DestroyList_L(L);
}
bool ListEmpty_L(LinkList L)
{
if(L == NULL)
return true;
return false;
}
void PriorElem_L(LinkList L,ElemType cur_e,ElemType &pre_e)
{
LNode *p;
p = L;
while( p->next != NULL && p->next->data != cur_e )
p = p->next;
if( p->next != NULL )
pre_e = p->data;
}
void NextElem_L(LinkList L, ElemType cur_e, ElemType &pre_e)
{
LNode *p = LocateElem_L(L,cur_e);
if( p->next != NULL )
pre_e = p->next->data;
}
void ListTraverse_L(LinkList L)
{
LNode *p;
p = L;
while( p->next != NULL)
{
cout<<p->data<<endl;
p = p->next;
}
cout<<p->data<<endl;
}
typedef LinkList LinkStack;
void InitStack_L(LinkStack &S){
S = NULL;
}//InitStack_L
void Push_L(LinkStack &S, ElemType e)
{
LNode *p = new LNode;
p->data = e;
p->next = S;
S = p;
}//Push_L
bool Pop_L(LinkStack &S, ElemType &e){
LNode *p;
if(S){
p = S;
S = S->next;
e = p->data;
delete p;
return true;
}//if
else return false;
}//Pop_L
void ClearStack_L(LinkStack &S){
ElemType e;
while(S){
Pop_L(S, e);
}//while
}//ClearStack_L
void DestroyStack_L(LinkStack &S){
ClearStack_L(S);
S = NULL;
}//DestroyStack_L
bool StackEmpty_L(LinkStack S){
if(S)
return false;
else
return true;
}//StackEmpty_L
bool GetTop_L(LinkStack S, ElemType &e){
if(S){
e = S->data;
return true;
}//if
else
return false;
}//GetTop_L
int StackLength_L(LinkStack S)
{
LNode *p = S;
int n = 0;
while(S){
n++;
S = S->next;
}
return n;
}
void StackTraverse_L(LinkStack S)
{
LNode *p = S;
while(p){
cout <<p->data<<endl;
}//while
}
#define MAX 256
typedef char * * HuffmanCode;
typedef struct {
int weight;
int lchild, rchild;
}HTNode;
typedef struct {
HTNode * HTree;
int root;
}HuffmanTree;
4 楼
pjfwarlock [专家分:0] 发布于 2006-12-28 12:25:00
void CreatHuffmanTree(HuffmanTree &HT, int *w, int n);
void HuffmanCoding(HuffmanTree HT, HuffmanCode &HC, int n);
void Coding(HuffmanTree T, int i, LinkStack &S, HuffmanCode &HC);
void Select (HTNode *HTree , int size , int &s1 , int &s2, int * &flag);
void StackTraverse(LinkStack &S, char *p);
void test(HuffmanCode HC){
return;
}
int main()
{
//从文件中读取字符并计数
FILE *fp,*fp1;
int a[2][MAX];
fp = fopen("text.txt", "rt");
if( fp == NULL ){
printf("can not open file: text.txt\n");
exit(0);
}//if
int ch,flag = 0;
int i, total = 0;
ch = fgetc(fp);
while(ch != EOF){
for(i = 0; i < total; i++)
if(a[0][i] == ch){
a[1][i]++;
flag = 1;
}//if
if(flag == 0)
a[0][total++] = ch;
a[1][total - 1] = 1;
ch = fgetc(fp);
}//while
fclose (fp);
//构造哈夫曼树
HuffmanTree HT;
HuffmanCode HC;
CreatHuffmanTree(HT, a[1], total);
HuffmanCoding(HT, HC, total);
fp = fopen("text.txt", "rt");
if( fp == NULL ){
printf("can not open file: text.txt\n");
exit(0);
}//if
fp1 = fopen("code.txt","w");
if( fp1 == NULL ){
printf("can not create file: code.txt\n");
exit(0);
}//if
ch = fgetc(fp);
while(ch != EOF){
flag = 0;
for(i = 0; i < total; i++)
if(a[0][i] == ch){
fputs(HC[i], fp1);
flag = 1;
break;
}//if
ch = fgetc(fp);
}//while
fclose(fp);
fclose(fp1);
delete [] HC;
return 1;
}
void CreatHuffmanTree(HuffmanTree &HT, int* w, int n){
if( n <= 1 )
return;
int m = 2 * n - 1;
HT.HTree = new HTNode[m];
HTNode *p;
int * fla = (int *)malloc(m * sizeof(int));
int i, s1, s2;
for(i = 0; i < m; ++i)
fla[i] = 0;
for(p = HT.HTree, i = 0; i < n; ++i){
p[i].weight = w[i];
p[i].lchild = -1;
p[i].rchild = -1;
}
for(;i <= m; ++i){
p[i].weight = 0;
p[i].lchild = -1;
p[i].rchild = -1;
}
for(i = n; i < m;++i){
Select(HT.HTree, i - 1, s1, s2,fla);
HT.HTree[i].lchild = s1;
HT.HTree[i].rchild = s2;
HT.HTree[i].weight = HT.HTree[s1].weight + HT.HTree[s2].weight;
}//for
HT.root = m - 1;
//free(fla);
//delete [] fla;
}//CreatHuffmanTree
5 楼
pjfwarlock [专家分:0] 发布于 2006-12-28 12:26:00
void Select (HTNode *HTree , int size , int &s1 , int &s2 , int * &flag)
{
int i,j,k,khelp;
for ( j = 0,k = 0, khelp = 0; j < 2 ; j++)
{
for ( i = 0 ; i <= size ; i++)
{
if (khelp == 0)
{
if (flag[i] == 0)
{
k = i;
khelp = 1;
}
else
continue;
}
else
if (flag[i] == 0 && HTree[i].weight < HTree[k].weight)
k = i;
}
if (j == 0)
s1 = k;
else
s2 = k;
flag[k] = 1;
k = 0;
khelp = 0;
}
}
void HuffmanCoding(HuffmanTree HT, HuffmanCode &HC, int n){
LinkStack S;
HC = new char *[n];
InitStack_L(S);
Coding(HT, HT.root, S, HC);
}
void Coding(HuffmanTree T, int i, LinkStack &S, HuffmanCode &HC){
char e;
if(T.HTree){
if((T.HTree[i].lchild == -1) && (T.HTree[i].rchild == -1)){
HC[i] = new char[StackLength_L(S) + 1];
// StackTraverse(S, HC[i]);
int z, n;
n = StackLength_L(S);
LNode *q = S;
z = n;
HC[i][z] = '\0';
for(z--; z >= 0; z--){
HC[i][z] = q->data;
q = q->next;
}//while
return;
}//if
Push_L(S, '0');
Coding(T, T.HTree[i].lchild, S, HC);
Pop_L(S, e);
Push_L(S, '1');
Coding(T, T.HTree[i].rchild, S, HC);
Pop_L(S, e);
}//if
}//Coding
/*
void StackTraverse(LinkStack &S, char *p)
{
int z, n;
n = StackLength_L(S);
LNode *q = S;
z = n;
p[i] = '\0';
for(z--; z >= 0; z--){
p[z] = q->data;
q = q->next;
}//while
}*/
6 楼
雪光风剑 [专家分:27190] 发布于 2006-12-28 13:19:00
看看fla的指针是不是已经指到了原本空间的外面
还有就是fla在create里释放的时候select是不是也同时停止了对fla的调用
这两点只要有一点出现问题就会引起错误
7 楼
pjfwarlock [专家分:0] 发布于 2006-12-28 22:31:00
debug时是没有问题的,但不能用Ctrl + F5运行,我用的是VC6.0
fla没有移动,Select那时按我的意思应该已经结束,另外:我同学用这段代码用
C++编程是没有问题的!我实在是不明白。是不是C 和 C++混编出现的问题?
8 楼
雪光风剑 [专家分:27190] 发布于 2006-12-29 04:44:00
“按你的意思”已经结束,那么是否select真的结束了对fla的访问
还有就是……c的语法在c++基本上都存在,哪来的混编一说……
9 楼
pjfwarlock [专家分:0] 发布于 2006-12-29 17:17:00
我也不知道啊?我的意思是一步步运行的话已经结束了,但换成机器语言我就不知道了。还有就是在c++用c (exp:using namespace....)和 在c中用c++ (cin,cout)是不一样的啊.能不能麻烦你运行一下我发的程序啊?
我来回复