IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

glibc detected double free or corruption


Sujet :

C

  1. #1
    Débutant Avatar de étoile de mer
    Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    978
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 978
    Points : 117
    Points
    117
    Par défaut glibc detected double free or corruption
    Bonjour à tous,
    quand je compile mon programme ya une bizarre erreur qui s'affiche glibc detected double free or corruption]
    je pense que c'est à cause de la fonction suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    int nbrLignesCommunes(ChainonMot const * pc,ChainonMot const * pc2)
    {
    /* Permettra de parcourir les deux liste (FR et AN) */
     ChainonCoord const *pcCoord;
    ChainonCoord const *pcCoord2;
    /* Permet de verifier si le numero de la ligne n'est pas deja compter dans la solution */
    int k;
     
    /* Tableau dynamique */
    int *LigneCommune = (int*) malloc (sizeof(int));
    int nombreDeSolution = 0;
     
     
     
    for(pcCoord = coord_GetPremierC(GetPtrCoordC(pc)) ; pcCoord!=NULL ; pcCoord=coord_GetNextC(pcCoord))
    	{
    	for(pcCoord2 = coord_GetPremierC(GetPtrCoordC(pc2)) ; pcCoord2!=NULL ; pcCoord2=coord_GetNextC(pcCoord2))
    		{
            if (GetLigne(pcCoord) == GetLigne(pcCoord2))
            {
     
                /* Se comportera comme un booleen */
                int nouvelleLigne = 1;
                 if(LigneCommune  == NULL)
                 {
                     printf("!!!!");
                     exit(1);
                }
     
    //
    //            /* On va chercher a savoir si la ligne est deja presente */
                for (k=0 ; k<nombreDeSolution ; k++)
                    if (LigneCommune[k] == GetLigne(pcCoord))
                        nouvelleLigne = 1;
     
    //
                if (nouvelleLigne)
                {
                    LigneCommune[nombreDeSolution] = GetLigne(pcCoord);
                    nombreDeSolution++;
                    int *Temporaire = realloc(LigneCommune, sizeof(int)*(nombreDeSolution + 1));
                    if (Temporaire != NULL)
                    {
                       free(LigneCommune);
                         LigneCommune = Temporaire;
                  }
                    else
                    {
                        printf ("Probleme lors du realloc !");
                        exit(1);
                    }
                }
     
            }
            }
    		}
     
    	return nombreDeSolution;
    }
    car quand je la met en commentaire, tout se passe nikel
    Une idée?
    merci
    Fichiers attachés Fichiers attachés

  2. #2
    Débutant Avatar de étoile de mer
    Profil pro
    Étudiant
    Inscrit en
    Avril 2007
    Messages
    978
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2007
    Messages : 978
    Points : 117
    Points
    117
    Par défaut
    Quand j'ellimine
    free(LigneCommune);
    ce message d'erreur disparait!
    ca veut dire quoi?

  3. #3
    Membre émérite Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Points : 2 280
    Points
    2 280
    Par défaut
    Bien sûr, c'est normal, quand un ré allocation réussi, tu n'as pas à libérer l'ancienne l'adresse, c'est le realloc qui gère ça pour toi.

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 381
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 381
    Points : 41 582
    Points
    41 582
    Par défaut
    Sans doute un problème au niveau de la gestion du tableau dynamique.
    Autant faire des fonctions séparées:
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    struct tableauDynamique_int
    {
    	int *tab;
    	size_t taille;
    	size_t capacite;
    };
     
    void TableauDynamique_int_ctor0(struct tableauDynamique_int *pThis);
    int TableauDynamique_int_ctor1(struct tableauDynamique_int *pThis, size_t capaciteInitiale);
    void TableauDynamique_int_dtor(struct tableauDynamique_int *pThis);
    void TableauDynamique_int_delete(struct tableauDynamique_int *pThis);
    struct tableauDynamique_int *TableauDynamique_int_new0(void);
    struct tableauDynamique_int *TableauDynamique_int_new1(size_t capaciteInitiale);
    int TableauDynamique_int_ajouter(struct tableauDynamique_int *pThis, int nouvelleValeur);
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    void TableauDynamique_int_ctor0(struct tableauDynamique_int *pThis)
    {
    	pThis->tab = NULL;
    	pThis->taille = 0;
    	pThis->capacite = 0;
    }
     
    int TableauDynamique_int_ctor1(struct tableauDynamique_int *pThis, size_t capaciteInitiale)
    {
    	int ret = -1;
    	pThis->tab = malloc(capaciteInitiale * sizeof *pThis->tab);
    	if(pThis->tab != NULL)
    	{
    		pThis->capacite = capaciteInitiale;
    		ret = 0;
    	}
    	else
    		pThis->capacite = 0;
    	pThis->taille = 0;
    	return ret;
    }
     
    void TableauDynamique_int_dtor(struct tableauDynamique_int *pThis)
    {
    	free(pThis->tab);
    	pThis->tab = NULL;
    	pThis->taille = 0;
    	pThis->capacite = 0;
    }
     
    void TableauDynamique_int_delete(struct tableauDynamique_int *pThis)
    {
    	if(pThis != NULL)
    	{
    		TableauDynamique_int_dtor(pThis);
    		free(pThis);
    	}
    }
     
    struct tableauDynamique_int *TableauDynamique_int_new0(void)
    {
    	struct tableauDynamique_int *pThis = malloc(sizeof *pThis);
    	if(pThis != NULL)
    		TableauDynamique_int_ctor0(pThis);
    	return pThis;
    }
     
    struct tableauDynamique_int *TableauDynamique_int_new1(size_t capaciteInitiale)
    {
    	struct tableauDynamique_int *pThis = malloc(sizeof *pThis);
    	if(pThis != NULL)
    	{
    		int ctorRet = TableauDynamique_int_ctor1(pThis, capaciteInitiale);
    		if(ctorRet < 0)
    		{
    			TableauDynamique_int_delete(pThis);
    			pThis = NULL;
    		}
    	}
    	return pThis;
    }
     
    int realloc_int(int **pp, size_t cElemNewSize)
    {
    	int ret = -1;
    	size_t cbNewSize = cElemNewSize * sizeof **pp;
    	int *tmp = realloc(*pp, cbNewSize);
    	if(tmp != NULL)
    	{
    		*pp = tmp;
    		ret = 0;
    	}
    	return ret;
    }
     
    int TableauDynamique_int_ajouter(struct tableauDynamique_int *pThis, int nouvelleValeur)
    {
    	assert(pThis->taille <= pThis->capacite);
     
    	/* Agrandir le tableau si besoin est. */
    	if(pThis->taille == pThis->capacite)
    	{
    		size_t nouvelleCapacite = (pThis->capacite < 4 ? 4 : (size_t)(pThis->capacite*1.5));
    		if(realloc_int(&pThis->tab, nouvelleCapacite) >= 0)
    		{
    			pThis->capacite = nouvelleCapacite;
    		}
    		else
    			return -1;
    	}
     
    	/* ajouter */
    	pThis->tab[ pThis->taille++ ] = nouvelleValeur;
    	return 0;
    }
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    int nbrLignesCommunes(ChainonMot const * pc,ChainonMot const * pc2)
    {
    	/* Permettra de parcourir les deux liste (FR et AN) */
    	ChainonCoord const *pcCoord;
    	ChainonCoord const *pcCoord2;
    	/* Permet de verifier si le numero de la ligne n'est pas deja compter dans la solution */
    	size_t k;
     
    	/* Tableau dynamique */
    	struct tableauDynamique_int lignesCommunes;
    	TableauDynamique_int_ctor0(&lignesCommunes);
     
    	for(pcCoord = coord_GetPremierC(GetPtrCoordC(pc)) ; pcCoord!=NULL ; pcCoord=coord_GetNextC(pcCoord))
    	{
    		for(pcCoord2 = coord_GetPremierC(GetPtrCoordC(pc2)) ; pcCoord2!=NULL ; pcCoord2=coord_GetNextC(pcCoord2))
    		{
    			if (GetLigne(pcCoord) == GetLigne(pcCoord2))
    			{
    				/* Se comportera comme un booleen */
    				int bNouvelleLigne = 1;
     
    				//
    				//            /* On va chercher a savoir si la ligne est deja presente */
    				for (k=0 ; k<lignesCommunes.taille ; k++)
    				{
    					if (lignesCommunes.tab[k] == GetLigne(pcCoord))
    						bNouvelleLigne = 0;
    				}
     
    				//
    				if (bNouvelleLigne)
    				{
    					int retAjout = TableauDynamique_int_ajouter(&lignesCommunes, GetLigne(pcCoord));
    					if(retAjout < 0)
    					{
    						printf ("Probleme lors du realloc !");
    						exit(1);
    					}
    				}
    			}
     
    		}/*for*/
    	}/*for*/
     
    	{
    		int nombreDeSolutions = (int)lignesCommunes.taille;
    		TableauDynamique_int_dtor(&lignesCommunes);
    		return nombreDeSolutions;
    	}
    }
    Edit: Mince, double-ninja. Enfin, pas grave.

Discussions similaires

  1. Réponses: 8
    Dernier message: 17/05/2019, 17h27
  2. Réponses: 6
    Dernier message: 06/05/2010, 23h57
  3. Réponses: 6
    Dernier message: 20/03/2009, 10h05
  4. [ProFTPd][glibc detected double free or corruption]
    Par Théolude dans le forum Administration système
    Réponses: 1
    Dernier message: 28/08/2008, 09h19
  5. erreur glibc detected double free or corruption.
    Par Screwt-K dans le forum C++
    Réponses: 1
    Dernier message: 02/07/2007, 16h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo