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

0x011C150A 处的第一机会错误(在 Project1.exe 中): 0xC0000005: 读取位置 0xDD2466E8 时发生访问冲突

2013-09-05 
0x011C150A 处的第一机会异常(在 Project1.exe 中): 0xC0000005: 读取位置 0xDD2466E8 时发生访问冲突。#in

0x011C150A 处的第一机会异常(在 Project1.exe 中): 0xC0000005: 读取位置 0xDD2466E8 时发生访问冲突。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
unsigned int weight;
unsigned int parent;
unsigned int lChild;
unsigned int rChild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;

void select(HuffmanTree HT,int Count,int s1,int s2)
{
    int i;
    unsigned int temp1=0;
    unsigned int temp2=0;
    unsigned int temp3;
    for(i=1;i<=Count;i++)
    {
        if(HT[i].parent==0)
        {
            if(temp1==0)
            {
                temp1=HT[i].weight;
                s1=i;
            }
            else
            {
                if(temp2==0)
                {
                    temp2=HT[i].weight;
                    s2=i;
                    if(temp2<temp1)
                    {
                        temp3=temp2;
                        temp2=temp1;
                        temp1=temp3;
                        temp3=s2;
                        s2=s1;


                        s1=temp3;
                    }
                }
                else
                {
                    if(HT[i].weight <= temp1)
                    {
                        temp2=temp1;
                        temp1=HT[i].weight;
                        s2=s1;
                        s1=i;
                    }
                    if(HT[i].weight>temp1&&HT[i].weight<temp2)
                    {
                        temp2=HT[i].weight;
                        s2=i;
                    }
                }
            }
        }
    }
}

void createHTree(HuffmanTree HT, int *w, int n)
{
    /*数组c[0..n-1]和w[0..n-1]存放了n个字符及其概率,构造霍夫树HT*/
    int i, s1, s2;
    if (n <= 1)
        return;
HT = (HuffmanTree)malloc(2*n*sizeof(HTNode));


    /*根据n个权值构造n棵只有根结点的二叉树*/
    for (i=1; i<=n; ++i)
    {
        HT[i].weight = w[i];
        HT[i].parent = HT[i].lChild = HT[i].rChild = 0;     
    }

/*构造霍夫曼树*/
    for (; i<2*n; ++i)
    {
/*从HT[1..i-1]中选择parent为0且weight最小的两棵树,其序号为s1和s2*/
select(HT,i-1,s1,s2);
        HT[s1].parent = i;    //将HT[i]作为新的二叉树的根节点
        HT[s2].parent = i;
        HT[i].lChild = s1;
        HT[i].rChild = s2;
        HT[i].weight = HT[s1].weight + HT[s2].weight;
    }

}

void createCode(HuffmanTree HT,HuffmanCode HC,int *w, int n){
char *cd;
int start=0,c=0,f=0;
HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cd = (char *)malloc(n*sizeof(char));
cd[n-1] = '\0';
for(int i = 1; i <= n; ++i){
start = n - 1;
for(c = i, f =HT[i].parent; f != 0; c = f, f = HT[f].parent){
if(HT[f].lChild == c)
cd[--start] = '0';
else
cd[--start] ='1';
}
HC[i] = (char *)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
free(cd);
}

void print(HuffmanTree &HT, int i) {     
int k;     
printf("\n构造出来的赫夫曼树是:");     
for(k = 1; k <= 2*i -1; ++k){         
printf("\n%3d,%3d,%3d,%3d,%3d",k,HT[k].weight,HT[k].rChild,HT[k].rChild,HT[k].lChild);     

}

void printcode(HuffmanTree HT,HuffmanCode HC,int n) {     
int j;     
printf("得到的各权Huffman编码是:\n");     
for(j=1; j <= n; j++){         
printf("\nwieght:%3d   Code:%s\n",HT[j].weight,HC[j]);   
}
}

void main()


{
int i;
int leaf_num;
int node_num;
int n;
HuffmanTree HT;
HuffmanCode HC;
int *pWeight;
printf("inut the number of the leaves:");
scanf("%d",&n);
pWeight = (int *)malloc(n*sizeof(char));
printf("input the numbers:");
for(i=0;i<n;i++){
scanf("%d",&pWeight[i]);
}
leaf_num = sizeof(pWeight)/sizeof(int);
node_num = leaf_num*2;
HT = (HuffmanTree)malloc(node_num*sizeof(HTNode));
HC =(HuffmanCode)malloc((leaf_num+1)*sizeof(char *));
createHTree(HT, pWeight, leaf_num);
createCode(HT, HC, pWeight, node_num);
printf("node  weight  parent  lchild  rchild\n");
print(HT,node_num);
printcode(HT,HC,n);
free(HT);
free(HC);
system("pause");
}
急救! c/c++ 异常
[解决办法]
leaf_num = sizeof(pWeight)/sizeof(int);//这句有错
node_num = leaf_num*2;
你的这两句永远都是leaf_num = 1;node_num = 2;
在createHTree函数中n<=1直接return了。
[解决办法]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

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

热点排行