Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    [C] Errore della funzione free()

    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 )
    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;
    
    }
    Errore :
    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

  2. #2
    scusa ... ma costa fa sto programma?

    l'ho compilato con dev-c++ e mi stampa questo:
    [8AVM]>>>


    che vuole? e dove ti da quell'errore?

    scusa ma non sono un esperto del C ... anzi!

  3. #3
    È l'inizio di un "interprete" Assembly 8086

  4. #4
    Credo che non abbia utilizzato la strcmp() in modo corretto.
    Dovresti scrivere
    codice:
    	if( strcmp(str, MOV)==0 )
    		mov(data);
    	else if(strcmp(str, PRINT)==0)
    		printf("AX = %d\nBX = %d\nCX = %d\nDX = %d\n", ax, bx, cx, dx);
    codice:
    	if(strcmp(reg, "ax")==0)
    		ax = value;
    	else if(strcmp(reg, "bx")==0)
    		bx = value;
    	else if(strcmp(reg, "cx")==0)
    		cx = value;
    	else if(strcmp(reg, "dx")==0)
    		dx = value;
    	else
    		return 0;
    Oppure, se preferisci ...

    codice:
    	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);
    codice:
    	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;
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Non e' uno strano errore ...

    Hai allocato un puntatore (data) in questa riga

    codice:
    if(!(data = calloc(17, sizeof(char)))){
    		printf("Error allocing <%p>", data);
    		exit(1);
    	}
    poi ne modifichi il valore con l'istruzione

    codice:
    ++data;
    e alla fine tenti di liberare la memoria con

    codice:
    free(data);
    ma il valore di data non è più quello originale ...

  6. #6
    @oregon : quindi che dovrei fare ? "Riavvolgerlo" ?

  7. #7
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Secondo te?

    Fanne una copia e modifica la copia, conservando l'originale ...!

  8. #8
    Si difatti funziona.

    Grazie mille a tutti !

    posto la soluzione ( solo per data )

    codice:
           char *data, *t_data;
    
           if(!(data = calloc(17, sizeof(char)))){
    		printf("Error allocing <%p>", data);
    		exit(1);
    	}
    	
    	t_data = data;
    /*Codice*/
    
    data = t_data;
    
    free(data);

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Il codice piu' "pulito" sarebbe

    codice:
    void parse(const char *str){
    
    	char *instruction;
    	char *data, *tmpdata;
    
    	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);
    	}
    
             tmpdata = data;
    	for(; *str; str++){
    		if(isspace(*str)){
    			*instruction = *str;
    
    			++instruction;
    		}
    		else{
    			*tmpdata = *str;
    			++tmpdata;
    		}
    	}
    
    	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);
    	
    }

  10. #10

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.