Il seguente codice non da errori di compilazione, ma quando lo eseguo mi da uno strano errore, che sembra essere generato dalla funzione free() ( Uso linux con compilatore gcc )
Errore :codice:#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define MOV "mov" #define PRINT "prt" int ax = 0; int bx = 0; int cx = 0; int dx = 0; int mov(const char *data); void parse(const char *str); int main(void){ char str[20]; while(1){ printf("[8AVM]>>> "); fgets(str, 20, stdin); parse(str); } return 0; } void parse(const char *str){ char *instruction; char *data; if(!(instruction = calloc(3, sizeof(char)))){ printf("Error allocing %s <%p>", instruction); exit(1); } if(!(data = calloc(17, sizeof(char)))){ printf("Error allocing <%p>", data); exit(1); } for(; *str; str++){ if(isspace(*str)){ *instruction = *str; ++instruction; } else{ *data = *str; ++data; } } if(strcmp(str, MOV)) mov(data); else if(strcmp(str, PRINT)) printf("AX = %d\nBX = %d\nCX = %d\nDX = %d\n", ax, bx, cx, dx); else printf("The instruction is not recognized\n"); free(data); free(instruction); } int mov(const char *data){ int value; char *reg; if(!(reg = calloc(2, sizeof(char)))){ printf("Error allocing <%p>", reg); exit(1); } for(; *data; data++){ if(isalpha(*data) && *data != ','){ *reg = *data; ++reg; } else if(isspace(*data)) continue; else if(isdigit(*data)) value = atoi(data); } if(strcmp(reg, "ax")) ax = value; else if(strcmp(reg, "bx")) bx = value; else if(strcmp(reg, "cx")) cx = value; else if(strcmp(reg, "dx")) dx = value; else return 0; free(reg); return 1; }
codice:[8AVM]>>> mov ax, 5 *** glibc detected *** ./8avm: free(): invalid pointer: 0x086a201f *** ======= Backtrace: ========= /lib/libc.so.6[0x4d1e0f18] /lib/libc.so.6(__libc_free+0x78)[0x4d1e43ef] ./8avm[0x8048714] ./8avm[0x8048578] /lib/libc.so.6(__libc_start_main+0xdc)[0x4d192724] ./8avm[0x80484b1] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 03:01 3862935 /home/marco/Esperimenti/8AVM/8avm 08049000-0804a000 rwxp 00000000 03:01 3862935 /home/marco/Esperimenti/8AVM/8avm 086a2000-086c3000 rwxp 086a2000 00:00 0 [heap] 4d15f000-4d160000 r-xp 4d15f000 00:00 0 [vdso] 4d160000-4d179000 r-xp 00000000 03:01 3600999 /lib/ld-2.4.so 4d179000-4d17a000 r-xp 00018000 03:01 3600999 /lib/ld-2.4.so 4d17a000-4d17b000 rwxp 00019000 03:01 3600999 /lib/ld-2.4.so 4d17d000-4d2aa000 r-xp 00000000 03:01 3601001 /lib/libc-2.4.so 4d2aa000-4d2ac000 r-xp 0012d000 03:01 3601001 /lib/libc-2.4.so 4d2ac000-4d2ad000 rwxp 0012f000 03:01 3601001 /lib/libc-2.4.so 4d2ad000-4d2b0000 rwxp 4d2ad000 00:00 0 4d574000-4d57f000 r-xp 00000000 03:01 3603333 /lib/libgcc_s-4.1.1-20060525.so.1 4d57f000-4d580000 rwxp 0000a000 03:01 3603333 /lib/libgcc_s-4.1.1-20060525.so.1 b7e00000-b7e21000 rw-p b7e00000 00:00 0 b7e21000-b7f00000 ---p b7e21000 00:00 0 b7f88000-b7f8a000 rw-p b7f88000 00:00 0 b7fa7000-b7fa9000 rw-p b7fa7000 00:00 0 bf992000-bf9a8000 rw-p bf992000 00:00 0 [stack] Abortito