c - error when reading file gcc version 4.4.7 but not gcc 6.1 -


i trying read file using fscanf, segmentation fault when try fclose() or free char* writing contents of file to. here code:

#include <stdlib.h> #include <stdio.h> #include <string.h>  int main(int argc, char **argv){     file *fp = fopen(argv[1],"r");     if(fp == null){         fprintf(stderr, "error: file not found.\n");         return 1;     }     char* directive = malloc(9);     while(fscanf(fp,"%s",directive) != eof){         printf("%s\n",directive);     }     //fclose(fp); seg fault     //free(directive); free(): invalid next size (fast)     return 0; } 

what more unusual when run code own computer gcc version 6.1 works fine, running on server gcc 4.4.7 doesn't work.

edit: file trying read

.byte   0000045d    00 .byte   00000457    09 .byte   00000458    09 .byte   00000141    0a .byte   00000183    0a .byte   000001ca    0a .byte   0000020d    0a .size   0800 .string "enter new char" .byte   00000251    0a .byte   00000262    0a .byte   00000263    0a .byte   0000029d    0a .byte   000002d3    0a .byte   0000030c    0a .byte   00000345    0a .byte   00000346    0a .byte   0000036d    0a .byte   000003b0    0a .byte   000003e9    0a .byte   00000409    0a .byte   00000442    0a 

i think have resolved it, realized directive being filled values larger 8 characters changed fcanf line to: fscanf(fp,"%s\t%x\t%x",directive,&mem_location,&value) – tom coda


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -