Liste doublement chaînée

Résolu
victorcoasne Messages postés 1101 Date d'inscription jeudi 24 avril 2003 Statut Membre Dernière intervention 23 juillet 2023 - 4 août 2007 à 22:23
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 - 6 août 2007 à 11:59
Bonjour,

J'ai créé une liste doublement chaînée mais celle-ci contient des erreurs de mémoire.

Pouvez-vous m'indiquez lesquelles ?

struct PreListView

{

    char* Col1;

    char* Col2;

    char* Col3;

    SYSTEMTIME Col4;

    char* Col5;

    PreListView* pPrecedent;

    PreListView* pSuivant;

};

PreListView* pPreListViewDebut=NULL;

PreListView* pPreListViewFin=NULL;


inline void VideListe()

{

    PreListView* pPreListViewActu = pPreListViewFin;

    while (pPreListViewActu!=NULL)

    {

        PreListView* pPreListViewActu2 = pPreListViewActu->pPrecedent;

        delete pPreListViewActu;

        pPreListViewActu = pPreListViewActu2;

    }

    pPreListViewFin = NULL;

    pPreListViewDebut = NULL;

}


inline bool AjoutListe(char* Colon1, char* Colon2, char* Colon3,

                       SYSTEMTIME Colon4, char* Colon5)

{

    PreListView* pPreListViewActu = NULL;

    if (pPreListViewDebut==NULL)

    {

        pPreListViewDebut = new PreListView;

        if (pPreListViewDebut==NULL)

            return false;

        pPreListViewDebut->pPrecedent = NULL;

        pPreListViewDebut->pSuivant = NULL;

        pPreListViewFin = pPreListViewDebut;

        pPreListViewActu = pPreListViewDebut;

    }

    else

    {

        PreListView* pPreListViewExplore = pPreListViewDebut;

        while (pPreListViewExplore!=NULL)

        {

            if (CompareSystemTime(pPreListViewExplore->Col4, Colon4)<0)

            {

                pPreListViewActu = new PreListView;

                if (pPreListViewActu==NULL)

                    return false;

                pPreListViewActu->pPrecedent = pPreListViewExplore->pPrecedent;

                pPreListViewExplore->pPrecedent = pPreListViewActu;

                pPreListViewActu->pSuivant= pPreListViewExplore;

                if (pPreListViewActu->pPrecedent != NULL)

                    pPreListViewActu->pPrecedent->pSuivant = pPreListViewActu;

                else

                    pPreListViewDebut = pPreListViewActu;


                pPreListViewExplore = NULL;

            }

            else

                pPreListViewExplore = pPreListViewExplore->pSuivant;

        }

        if (pPreListViewActu==NULL)

        {

            pPreListViewFin->pSuivant = new PreListView;

            if (pPreListViewFin->pSuivant==NULL)

                return false;

            pPreListViewActu = pPreListViewFin->pSuivant;

            pPreListViewActu->pPrecedent = pPreListViewFin;

            pPreListViewFin = pPreListViewActu;

        }

    }

    pPreListViewActu->Col1 = NULL;

    pPreListViewActu->Col2 = NULL;

    pPreListViewActu->Col3 = NULL;

    pPreListViewActu->Col4 = Colon4;

    pPreListViewActu->Col5 = NULL;


    pPreListViewActu->Col1 = new char[strlen(Colon1)+1];

    if (pPreListViewActu->Col1==NULL)

        return false;

    pPreListViewActu->Col1[0]=0;

    strcpy(pPreListViewActu->Col1, Colon1);


    pPreListViewActu->Col2 = new char[strlen(Colon2)+1];

    if (pPreListViewActu->Col2==NULL)

        return false;

    pPreListViewActu->Col2[0]=0;

    strcpy(pPreListViewActu->Col2, Colon2);


    pPreListViewActu->Col3 = new char[strlen(Colon3)+1];

    if (pPreListViewActu->Col3==NULL)

        return false;

    pPreListViewActu->Col3[0]=0;

    strcpy(pPreListViewActu->Col3, Colon3);


    pPreListViewActu->Col5 = new char[strlen(Colon5)+1];

    if (pPreListViewActu->Col5==NULL)

        return false;

    pPreListViewActu->Col5[0]=0;

    strcpy(pPreListViewActu->Col5, Colon5);


    return true;

}


void AfficheListe()

{

    PreListView* pPreListViewActu = pPreListViewDebut;

    while (pPreListViewActu != NULL)

    {

        cout << pPreListViewActu->Col1 << " - " << pPreListViewActu->Col2 << " - " << pPreListViewActu->Col3 << " - " << " - " << pPreListViewActu->Col5;

        pPreListViewActu = pPreListViewActu->pSuivant;

    }

}

Merci d'avance et bonne prog,
@++

Le créateur du site http://victorlogiciels.com

10 réponses

gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010
6 août 2007 à 10:18
Tu as oublié une instruction en rouge. En plus je comprends pas pourquoi tu utilises une fonction inline pour l'ajout d'un element 




    else
    {
        PreListView* pPreListViewExplore= pPreListViewDebut;
        for (int i=0;pPreListViewExplore!=NULL&&i<NbInsert;i++)
        {
            if (CompareSystemTime(pPreListViewExplore->Col4, Colon4)<0)
            {
                pPreListViewActu->pPrecedent = pPreListViewExplore->pPrecedent;
                pPreListViewActu->pSuivant = pPreListViewExplore;
                if (pPreListViewExplore->pPrecedent != NULL)
                    pPreListViewExplore->pPrecedent->pSuivant = pPreListViewActu;
                else
                    pPreListViewDebut = pPreListViewActu;
                pPreListViewExplore->pPrecedent = pPreListViewActu;
                NbInsert++;
                return true;
            }
            pPreListViewExplore = pPreListViewExplore->pSuivant;
            if (pPreListViewExplore!=NULL&&i+1>=NbInsert)
            {
                MessageBox(0, "Erreur grave de gestion de la mémoire !",
                              "Erreur Interne !", 16);
                return false;
            }
            else if (pPreListViewExplore==NULL&&i+1!=NbInsert)
            {
                MessageBox(0, "Erreur grave de gestion de la mémoire (3) !",
                              "Erreur Interne !", 16);
                return false;
            }
        }
        pPreListViewFin->pSuivant = pPreListViewActu;
         pPreListViewActu->pPrecedent = pPreListViewFin;
        pPreListViewFin = pPreListViewActu;
    }
    NbInsert++;
    return true;
3
caiman125 Messages postés 36 Date d'inscription mercredi 27 décembre 2006 Statut Membre Dernière intervention 25 novembre 2010
5 août 2007 à 02:26
Salut ma tout premiere remarque est

PreListView* pPreListViewDebut=NULL;
PreListView* pPreListViewFin=NULL;

ses deux lignes doit etre dans une fonction

et si tu peut indique les type d'erreurs
0
victorcoasne Messages postés 1101 Date d'inscription jeudi 24 avril 2003 Statut Membre Dernière intervention 23 juillet 2023 7
5 août 2007 à 11:38
Bonjour,

Pourquoi dans une fonction ?
Si je veux ajouter plusieurs objets en executant plusieurs fois la fonction ajouté sur action de l'utilisateur autant le laisser en global.

L'erreur c'est un plantage, un bug, une erreur d'exécution lié à une mauvaise gestion de la mémoire.

Merci et bonne prog,
@++

Le créateur du site http://victorlogiciels.com
0
caiman125 Messages postés 36 Date d'inscription mercredi 27 décembre 2006 Statut Membre Dernière intervention 25 novembre 2010
5 août 2007 à 12:39
je veux dire la fonction "main" au moins
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
victorcoasne Messages postés 1101 Date d'inscription jeudi 24 avril 2003 Statut Membre Dernière intervention 23 juillet 2023 7
5 août 2007 à 13:43
Bonjour,

C'est pas ça qui fait interférence je te rassure.

Merci et bonne prog,
@++

Le créateur du site http://victorlogiciels.com
0
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
5 août 2007 à 18:46
Quand l'élément que tu ajoutes est le dernier (code : if (pPreListViewActu==NULL), vers la fin de AjouteListe), tu as oublié d'initialiser ->pSuivant à 0 donc ça fout le bordel plus tard.

J'ai testé : on dirait que ça marche.

Encore un truc : oublie pas de désalllouer la mémoire des pointeurs (->Colx) dans VideListe() sinon bonjour les fuites.
0
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
5 août 2007 à 19:03
ben y a eu un problème de police, désolé.
0
victorcoasne Messages postés 1101 Date d'inscription jeudi 24 avril 2003 Statut Membre Dernière intervention 23 juillet 2023 7
5 août 2007 à 21:46
Bonjour,

Je l'ai refait en class mais problème d'insertion.
Plus de plantage mémoire mais tout les membres insérés ne s'affichent pas !
La messagebox erreur 4 s'affiche mais bizarement pas la 2 !

class Trieur
{
    private:
        struct PreListView
        {
            char* Col1;
            char* Col2;
            char* Col3;
            SYSTEMTIME Col4;
            char* Col5;
            PreListView* pPrecedent;
            PreListView* pSuivant;
        };
        PreListView* pPreListViewDebut;
        PreListView* pPreListViewFin;
        int NbInsert;
    public:
        Trieur();
        ~Trieur();
        inline void AfficheListe();
        inline bool Ajouter(char* Colon1, char* Colon2, char* Colon3,
                     SYSTEMTIME Colon4, char* Colon5);
};

Trieur::Trieur()
{
    pPreListViewDebut=NULL;
    pPreListViewFin=NULL;
    NbInsert=0;
}
Trieur::~Trieur()
{
    delete pPreListViewDebut;
    pPreListViewDebut=NULL;
    pPreListViewFin=NULL;
}

inline bool Trieur::Ajouter(char* Colon1, char* Colon2, char* Colon3,
                     SYSTEMTIME Colon4, char* Colon5)
{
    /* Préparation de l'entrée à ajouter */
    PreListView* pPreListViewActu=NULL;
    pPreListViewActu = new PreListView;
    if (pPreListViewActu==NULL)
        return false;

    pPreListViewActu->pPrecedent = NULL;
    pPreListViewActu->pSuivant = NULL;
    pPreListViewActu->Col1 = NULL;
    pPreListViewActu->Col2 = NULL;
    pPreListViewActu->Col3 = NULL;
    pPreListViewActu->Col4 = Colon4;
    pPreListViewActu->Col5 = NULL;

    pPreListViewActu->Col1 = new char[strlen(Colon1)+1];
    if (pPreListViewActu->Col1==NULL)
        return false;
    pPreListViewActu->Col1[0]=0;
    strcpy(pPreListViewActu->Col1, Colon1);

    pPreListViewActu->Col2 = new char[strlen(Colon2)+1];
    if (pPreListViewActu->Col2==NULL)
        return false;
    pPreListViewActu->Col2[0]=0;
    strcpy(pPreListViewActu->Col2, Colon2);

    pPreListViewActu->Col3 = new char[strlen(Colon3)+1];
    if (pPreListViewActu->Col3==NULL)
        return false;
    pPreListViewActu->Col3[0]=0;
    strcpy(pPreListViewActu->Col3, Colon3);

    pPreListViewActu->Col5 = new char[strlen(Colon5)+1];
    if (pPreListViewActu->Col5==NULL)
        return false;
    pPreListViewActu->Col5[0]=0;
    strcpy(pPreListViewActu->Col5, Colon5);
   
    /* Ajout de l'entrée */
    if (pPreListViewDebut==NULL)
    {
        pPreListViewDebut = pPreListViewActu;
        pPreListViewFin = pPreListViewActu;
    }
    else
    {
        PreListView* pPreListViewExplore=pPreListViewDebut;
        for (int i=0;pPreListViewExplore!=NULL&&i<NbInsert;i++)
        {
            if (CompareSystemTime(pPreListViewExplore->Col4, Colon4)<0)
            {
                pPreListViewActu->pPrecedent = pPreListViewExplore->pPrecedent;
                pPreListViewActu->pSuivant = pPreListViewExplore;
                if (pPreListViewExplore->pPrecedent != NULL)
                    pPreListViewExplore->pPrecedent->pSuivant = pPreListViewActu;
                else
                    pPreListViewDebut = pPreListViewActu;
                pPreListViewExplore->pPrecedent = pPreListViewActu;
                NbInsert++;
                return true;
            }
            pPreListViewExplore = pPreListViewExplore->pSuivant;
            if (pPreListViewExplore!=NULL&&i+1>=NbInsert)
            {
                MessageBox(0, "Erreur grave de gestion de la mémoire !",
                              "Erreur Interne !", 16);
                return false;
            }
            else if (pPreListViewExplore==NULL&&i+1!=NbInsert)
            {
                MessageBox(0, "Erreur grave de gestion de la mémoire (3) !",
                              "Erreur Interne !", 16);
                return false;
            }
        }
        pPreListViewFin->pSuivant = pPreListViewActu;
        pPreListViewFin = pPreListViewActu;
    }
    NbInsert++;
    return true;
}

inline void Trieur::AfficheListe()
{
    PreListView* pPreListViewExplore=pPreListViewDebut;
    for (int i=0;pPreListViewExplore!=NULL&&i<NbInsert;i++)
    {
        cout << pPreListViewExplore->Col1 << pPreListViewExplore->Col2 <<  pPreListViewExplore->Col3 <<                   pPreListViewExplore->Col5);
        pPreListViewExplore = pPreListViewExplore->pSuivant;
        if (pPreListViewExplore!=NULL&&i+1>=NbInsert)
        {
            MessageBox(0, "Erreur grave de gestion de la mémoire (2) !",
                          "Erreur Interne !", 16);
            return;
        }
        else if (pPreListViewExplore==NULL&&i+1!=NbInsert)
        {
            MessageBox(0, "Erreur grave de gestion de la mémoire (4) !",
                          "Erreur Interne !", 16);
            return;
        }
    }
}

Si je met en commentaire :
                pPreListViewActu->pPrecedent = pPreListViewExplore->pPrecedent;
                pPreListViewActu->pSuivant = pPreListViewExplore;
                if (pPreListViewExplore->pPrecedent != NULL)
                    pPreListViewExplore->pPrecedent->pSuivant = pPreListViewActu;
                else
                    pPreListViewDebut = pPreListViewActu;
                pPreListViewExplore->pPrecedent = pPreListViewActu;
                NbInsert++;
                return true;
là ça marche.

Je précise que CompareSystemTime est une fonction perso qui compare le system time passé en 2ème par rapport en premier et renvoi 1 si supérieur, 0 si égal et -1 si inférieur

Merci et bonne prog,
@++

Le créateur du site http://victorlogiciels.com
0
victorcoasne Messages postés 1101 Date d'inscription jeudi 24 avril 2003 Statut Membre Dernière intervention 23 juillet 2023 7
6 août 2007 à 11:06
Bonjour,

Ok Merci.

J'utilise inline parceque avoir 1ko de plus ça ne me dérange pas je privilégie la vitesse.

Merci et bonne prog,
@++

Le créateur du site http://victorlogiciels.com
0
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
6 août 2007 à 11:59
Ouais mais bon quand la fonction fait une certaine taille (en temps d'exécution) c'est pas les quelques instructions d'appel qui vont retarder beaucoup.
0
Rejoignez-nous