考虑以下代码:
FILE *input;
FILE *output;
input=fopen("in.txt", "r");
output=fopen("out.txt", "w");
char buffer[1000];
char bytebuffer=0;
char tempchr=0;
char huffmancode[100]={0};
int bufferindex=7;
fgets(&buffer[0],255,input);
int length=0;
while (buffer[length]!=0) length++;
fputc(length,output);
int j;
int k;
for (j=0;j<length;j++){
tempchr=buffer[j];
strcpy(&huffmancode[0],code[tempchr-97]);
k=0;
while(huffmancode[k]!=0){
if (huffmancode[k]!='0'){
setBit(&bytebuffer,bufferindex);
}
bufferindex-=1;
if (bufferindex==-1){
fputc(bytebuffer,output);
bytebuffer=0;
bufferindex=7;
}
k++;
}
}
由于这一行,此代码无法正常运行:
tempchr=buffer[j];
这条线工作得很好,j < 10
但是j >= 10
程序抛出了一个状态访问溢出异常。缓冲区变量被分配了 1000 个字节,所以不清楚为什么程序无法访问边界内的索引。为什么会失败?
有一些我没有包含的数组定义。我认为它们不是问题,但无论如何它们都在这里:
agac
数组:
int agac[1000]={21,12,9,7,5,
5,4,0,0,0,
0,3,2,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
};
sembol
数组:
char sembol[1000]={0,0,0,'a','b',
0,'f',0,0,0,
0,'u','k',0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
};
霍夫曼码数组:
char *code[27]={ "00","01",0,0,0,
"11",0,0,0,0,
"101",0,0,0,0,
0,0,0,0,"100",
0,0,0,0,0,
0,0
};