-
unable to free string
When trying to execute this piece of code, the program craches when it reaches free(zin). When I remove the free(zin), everything works well, but I think it's better to free the allocated memory. Does anyone know what I'm doing wrong ?
Code:
void readFile(){
FILE *f;
char *zin, *tag;
if ( !(f = fopen("test.txt", "r")) ) return;
zin = malloc(sizeof(char));
while (fgets(zin, SKY_MAX_ZIN_SIZE, f) != NULL){
tag = fileZoekTag (zin);
fileProcessTag(tag);
}
free (zin);
fclose(f);
}
-
Code:
zin = malloc(sizeof(char));
Did you intend to allocate more than one byte? If SKY_MAX_ZIN_SIZE is bigger than 1, you are lying to fgets. So the loop will either write beyond memory you can write to, or the loop will never go anywhere.
-
That was the problem indeed. Thanks.