C语言 读取位置 发生冲突

#include"stdio.h"
#include<cmath>
#include<string.h>

#define N 256

//This is the called function.This function is used to compute the entropy of the whole text.
float log2(float index)
{
return log(index)/log(2.0f);
}
//build a function of log2()
float Entropy(float Sa)
{
float H;
H=-Sa*log2(Sa);
return(H);
}
//compute the entropy

int main()
{
//open the file
FILE *fp;
char ch;
int i;
int index;
int sum=0;
float Hs=0.0;

fp=fopen("C:\\669HOMEWORK\\material\\text.dat","rt");
if(fp==NULL)
{
printf("Can not open the file!\n");
return 1;
}
/***********************************************************************
read the text
************************************************************************/
struct TextPoint
{
float prob;
char lr;

}sym[N];
//any ASCII which is exsited in the text has to computer its probability and
//show the letter's name in the screen.lr=letter.
ch=fgetc(fp);
printf("The Answer of Problem 1.1.(1) is:\n");

while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);

sum++;
index=(int)(ch-'NUL');
sym[index].prob++;
sym[index].lr=ch;

for(i=0;i<N;i++)
{
Hs+=Entropy((sym[i].prob)/sum);//compute the entropy.
printf("The probability of %s is %f\n",sym[i].lr,(sym[i].prob)/sum);

}
printf("The entropy is %f",Hs);
}
fclose(fp);

return 0;
}
生成解决方案成功,但运行总说我“try3.exe 中的 0x004116b5 处最可能的异常: 0xC0000005: 读取位置 0xfda04d0c 时发生访问冲突”这是什么意思啊?请大家帮帮忙啊。。。泪奔

第1个回答  2010-02-01
没有仔细看代码,怀疑是内存泄露或指针使用的问题,建议单步跟踪测试,以确定故障点。
第2个回答  2010-02-01
有好几个地方要改:
1.在#define N 256语句下面添上:
#define NUL '\0'
2.在while(ch!=EOF)语句前添上:
memset(sym,0x0,sizeof(sym));
3.index=(int)(ch-NUL);改为:
index=(int)(ch-NUL);
4.printf("The probability of %s is %f\n",sym[i].lr,(sym[i].prob)/sum);改为:
printf("The probability of %c is %f\n",sym[i].lr,(sym[i].prob)/sum);
---------------------------------------------------------
#include"stdio.h"
#include<cmath>
#include<string.h>

#define N 256
#define NUL '\0'/////////////////////////////////////////

//This is the called function.This function is used to compute the entropy of the whole text.
float log2(float index)
{
return log(index)/log(2.0f);
}
//build a function of log2()
float Entropy(float Sa)
{
float H;
H=-Sa*log2(Sa);
return(H);
}
//compute the entropy

int main()
{
//open the file
FILE *fp;
char ch;
int i;
int index;
int sum=0;
float Hs=0.0;

fp=fopen("C:\\text.dat","rt");
if(fp==NULL)
{
printf("Can not open the file!\n");
getchar();
return 1;
}
/***********************************************************************
read the text
************************************************************************/
struct TextPoint
{
float prob;
char lr;

}sym[N];
//any ASCII which is exsited in the text has to computer its probability and
//show the letter's name in the screen.lr=letter.
ch=fgetc(fp);
printf("The Answer of Problem 1.1.(1) is:\n");
memset(sym,0x0,sizeof(sym));///////////////////////////////////
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);

sum++;
index=(int)(ch-NUL);///////////////////
sym[index].prob++;
sym[index].lr=ch;

for(i=0;i<N;i++)
{
Hs+=Entropy((sym[i].prob)/sum);//compute the entropy.
printf("The probability of %c is %f\n",sym[i].lr,(sym[i].prob)/sum);/////////////////////

}
printf("The entropy is %f",Hs);
}
fclose(fp);
getchar();
return 0;
}本回答被提问者采纳
第3个回答  2010-02-01
调试时使用了什么IDE

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网