Erreur de segmentation(comme tous les newbie)

Fermé
Vanatou - 19 janv. 2008 à 18:36
tatou_38 Messages postés 1928 Date d'inscription vendredi 21 avril 2006 Statut Membre Dernière intervention 5 août 2015 - 19 janv. 2008 à 18:43
Bonjour,

Je réalise en C un simulateur de gestion de mémoire (donc en cours de dvlpt) et j'ai une erreur de segmentation à l'exécution, qqn pourrait-il m'aider svp?? Car je ne vois pas d'où peut venir l'erreur j'ai bien fait des malloc pour réserver l'espace mémoire nécessaire... (Je mets en gras dans le code la ligne où ca bloque...)

Pour l'exécution c'est dmm_sim_ff --memsize 500 truc.txt
et truc ressemble à :

malloc a 3
malloc b 5
malloc c 8
free b
malloc d 4
malloc e 7
malloc f 1
free c
free a
malloc g 5
malloc h 6
malloc i 3
malloc j 4
free d
free g
malloc k 2
malloc l 6
malloc m 2





Je joins le code:






#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <limits.h>
#include <getopt.h>
#include "parseur.c"

/*déclaration des variables globales
*/
int MEMSIZE=0;


/* On créé ici, une structure de type noeud, ceci sert
à savoir où se trouve un élément donné dans notre memoire
mais aussi à en connare la taille.
*/
typedef struct node{
char* id;
int addr;
struct node *next;
} node;

int parse_Input(FILE * input);

int lecture_ligne(char * line);

int algo_Malloc(char* identifiant,int taille);

int algo_Free(char* identifiant);

int main(int argc, char **argv)
{

char *inputfn=NULL;
FILE *input;
int c;



while (1)
{
int option_index=0;

static struct option long_options[] =
{
{"memsize",1,0,0},
{"print-memory",0,0,0},
{0,0,0,0}
};

c = getopt_long(argc, argv, "t:", long_options, &option_index);
if (c==-1)
break;

switch (c)
{
case 0:
fprintf (stderr,"option %s", long_options[option_index].name);
if (optarg)
fprintf (stderr," avec argument %s", optarg);
fprintf (stderr,"\n");
if (long_options[option_index].name=="memsize"){
if(optarg)
MEMSIZE=atoi(optarg);
fprintf(stderr,"Valeur de MEMSIZE : %d \n",MEMSIZE);
}
break;

case 't':
fprintf(stderr,"je recupere des trucs avec le t\n");
inputfn = optarg;
break;

case '?':
break;

default:
fprintf (stderr,"?? caractère de code 0%o ??\n", c);
}
}
if (optind < argc)
{
fprintf (stderr,"Arguments ne constituant pas des options: ");
while (optind < argc)
fprintf (stderr,"%s ", argv[optind++]);
fprintf (stderr,"\n");
}

if (inputfn == NULL) {
fprintf (stderr, "No input file .txt.\n");
return(EXIT_FAILURE);
}


/*Open input file*/
input = fopen(inputfn,"r+");
/*appel à la fonction parseur qui va lire le fichier définit par input et ranger
les données extraites du fichier dans une structure du type triple_string définit
par parseur.c
*/
fprintf(stderr,"passe ouverture fichier\n");
int result_parseur;
result_parseur=parse_Input(input);
fprintf(stderr,"fin de la fonction parse_Input\n");
if(result_parseur==EXIT_SUCCESS){
fprintf(stderr,"Traitement réussi\n");
return EXIT_SUCCESS;
}
else{
fprintf(stderr,"Echec du traitement\n");
return EXIT_FAILURE;
}
}

/*

*/
int parse_Input(FILE * input){
fprintf(stderr,"je rentre dans parse_Input\n");
char *line;
line=malloc(LINE_MAX*sizeof(char));
/*Read */
//nl représente le nombre de ligne du fichier d'entrée
int nl=0;
int resu;
fprintf(stderr,"debut de lecture\n");
fprintf(stderr,"valeur de LINE_MAX:%d \n",LINE_MAX);
while (fgets(line, LINE_MAX, input)!=NULL){
nl++;
init_parsers();
fprintf(stderr,"%c%c%c%c%c%c%c%c%c\n",line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8]);
resu=lecture_ligne(line);
close_parsers();

}
return resu;
if (line!=NULL){

free(line);
}
}

int algo_Malloc(char* identifiant,int taille){
return taille;
}

int algo_Free(char* identifiant){
return 1;
}

int lecture_ligne(char * line){
fprintf(stderr,"fin de lecture\n");
fprintf(stderr,"fin declaration tableau de triple_string\n");
int j;
int result=0;
int taille=0;
int resu=0;
fprintf(stderr,"debut de la boucle\n");

for(j=0;j<20;j++){
triple_string *data_in;
data_in=malloc(sizeof(triple_string));
fprintf(stderr,"c'est la que ca bloque\n");
result=parse_sim(line,data_in);
fprintf(stderr,"avant switch");
switch(result){
case '1':
taille=atoi(data_in->s3);
resu=algo_Malloc(data_in->s2,taille);
break;
case '2':
resu=algo_Free(data_in->s2);
break;
case '3':
break;
case '4':
fprintf(stderr,"Error, not able to recognize line %d",j);
return EXIT_FAILURE;
break;
case '?':
fprintf(stderr,"Error in parser, unrecognized output from parser");
return EXIT_FAILURE;
break;
}
fprintf(stderr,"après switch");
if (data_in!=NULL){
free(data_in);
}
}
fprintf(stderr,"fin de la boucle\n");
return EXIT_SUCCESS;
}







ainsi que le parseur.c qui va avec : (ce n'est pas moi qui l'ai écrit)









#define _GNU_SOURCE
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>

/* A simple data structure to store three strings */
typedef struct triple_string {
char* s1;
char* s2;
char* s3;
} triple_string ;

/* A simple function to print a triple_string */
void triple_string_print(triple_string* input){
if (input->s1 != NULL) printf("%s ", input->s1);
if (input->s2 != NULL) printf("%s ", input->s2);
if (input->s3 != NULL) printf("%s", input->s3);
printf("\n");
}

/* A simple function to reset a triple_string */
void triple_string_reset(triple_string* input){
// clean memory
if (input->s1 != NULL) free (input->s1);
if (input->s2 != NULL) free (input->s2);
if (input->s3 != NULL) free (input->s3);
input->s1 = NULL;
input->s2 =NULL;
input->s3 =NULL;
}

regex_t preg1;
regex_t preg2;
regex_t preg3;
regex_t preg4;
/** This function initializes preg1,preg2,preg3,preg4 */
void init_parsers()
{
// compilation of the regexp cooresponding to *malloc* lines
regcomp(&preg1, "^:space:*(malloc):space:+([a-z0-9]+):space:+([0-9]+):space:*$",REG_EXTENDED|REG_ICASE);

// compilation of the regexp cooresponding to *free* lines
regcomp(&preg2, "^:space:*(free):space:+([a-z0-9]+):space:*$",REG_EXTENDED|REG_ICASE);

// compilation of the regexp cooresponding to *comment* lines
regcomp(&preg3, "^:space:*#.*$",REG_EXTENDED|REG_ICASE);

// compilation of the regexp cooresponding to defrag lines
regcomp(&preg4, "^:space:*([a-z0-9]+):space:+([0-9]+):space:+([0-9]+):space:*$",REG_EXTENDED|REG_ICASE);

}

/** This function cleans the memory w.r.t to preg1,preg2,preg3 */
void close_parsers()
{
regfree(&preg1);
regfree(&preg2);
regfree(&preg3);
regfree(&preg4);
}

/* This function parses an input line w.r.t the simulator syntax and fills r accordingly.
* It returns the type of the line if found, -1 if it fails to recognize a line
* See the BE 2007 of OS Architecture @ ENSIETA
*/
int parse_sim(const char* input, triple_string* r) {

//needed variable
regmatch_t matched[4];
triple_string_reset(r);

// test if we have a *malloc* line
if (!regexec( &preg1, input, 4, matched, 0)) {
// we use strdup to add a null byte at the end of the matched part
r->s1 = strndup(input+matched[1].rm_so,matched[1].rm_eo-matched[1].rm_so);
r->s2 = strndup(input+matched[2].rm_so,matched[2].rm_eo-matched[2].rm_so);
r->s3 = strndup(input+matched[3].rm_so,matched[3].rm_eo-matched[3].rm_so);
return 1;
};

// test if we have a *free* line
if (!regexec( &preg2, input, 4, matched, 0)) {
r->s1=strndup(input+matched[1].rm_so,matched[1].rm_eo-matched[1].rm_so);
r->s2=strndup(input+matched[2].rm_so,matched[2].rm_eo-matched[2].rm_so);
return 2;
};

// test if we have a *commen*t line
if (!regexec( &preg3, input, 1, matched, 0)) {
return 3;
}
// error, we are not able to recognize anything
// we *must* reallocate the strings in r
// in order to make the next free

return 4;

}


/* This function parses an input line w.r.t the defrag syntax and fills r accordingly.
* It returns the type of the line if found, -1 if it fails to recognize a line
* See the BE 2007 of OS Architecture @ ENSIETA
*/
int parse_defrag(const char* input, triple_string* r) {

//needed variable
regmatch_t matched[4];
triple_string_reset(r);

// test if we have a regular defrag line
if (!regexec( &preg4, input, 4, matched, 0)) {
// we use strdup to add a null byte at the end of the matched part
r->s1=strndup(input+matched[1].rm_so,matched[1].rm_eo-matched[1].rm_so);
r->s2=strndup(input+matched[2].rm_so,matched[2].rm_eo-matched[2].rm_so);
r->s3=strndup(input+matched[3].rm_so,matched[3].rm_eo-matched[3].rm_so);
return 4;
};

// test if we have a *commen*t line
if (!regexec( &preg3, input, 1, matched, 0)) {
return 3;
}

// error, we are not able to recognize anything
return -1;

}


/*
#define TEST_PARSER_SIM(X) if (parse_sim(X,&r)<0) printf("SIM: Cannot parse: %s\n",X);else triple_string_print(&r);

#define TEST_PARSER_DEFRAG(X) if (parse_defrag(X,&r)<0) printf("DEFRAG: Cannot parse: %s\n",X);else triple_string_print(&r);

int main() {

triple_string r;
r.s1 = NULL;
r.s2 = NULL;
r.s3 = NULL;

init_parsers();

printf("Test parser_sim\n");
TEST_PARSER_SIM(" malloc r5 5 ")
TEST_PARSER_SIM(" # sdfsgdfg ")
TEST_PARSER_SIM("# another comment ")
TEST_PARSER_SIM(" free x ")
TEST_PARSER_SIM(" toto ")

printf("Test parser_defrag\n");
TEST_PARSER_DEFRAG(" x1 6 5 ")
TEST_PARSER_DEFRAG(" # sdfsgdfg ")
TEST_PARSER_DEFRAG("# another comment ")
TEST_PARSER_DEFRAG(" free x ")
TEST_PARSER_DEFRAG("spo 5 9")

printf("Test of memory leak\n");
int i=0;
while (i++<100000) { TEST_PARSER_SIM(" malloc r5 5 ") }*/
/*
close_parsers();

return 0;
}*/

1 réponse

tatou_38 Messages postés 1928 Date d'inscription vendredi 21 avril 2006 Statut Membre Dernière intervention 5 août 2015 120
19 janv. 2008 à 18:43
As-tu essayé avec un débugger ?
1