Monday, April 16, 2012

Status access overflow while accessing array of chars

    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++;
}

}


this code is failing due



tempchr=buffer[j];


It works perfectly fine for j<10 but for j>=10 the program throws a status access overflow exception. buffer variable is allocated for 1000 bytes so I don't have a clue why the program is failing to access an index in bounds. Why is this failing?



There are some array definitions which I didn't include. I don't think they are the problem but here are they anyways.



    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
};
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
};




No comments:

Post a Comment