微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更正与结构指针相关的溢出问题?

如何解决如何更正与结构指针相关的溢出问题?

由于时间太长,我没有共享我的所有代码,所以我希望您可以帮助我完成使用valgrind进行的调试以及它的溢出问题。 我只是想在另一个函数中检索在此尖形结构中分配的数据。

struct paramAL{
    int noVersion;
    int noligne;
    char tligne[100];
    };

这是readTrans函数的一部分,在其中我将数据分配给了指向的结构

void* readTrans(char* nomFichier){
        FILE *f;
        char buffer[100];
        char *tok,*sp;
        pthread_t tid[1000];
        int nbThread = 0;
        int i;
    
        //Ouverture du fichier en mode "r" (equiv. "rt") : [r]ead [t]ext
        f = fopen(nomFichier,"rt");
        if (f==NULL)
            error(2,"readTrans: Erreur lors de l'ouverture du fichier.");
    
        //Lecture (tentative) d'une ligne de texte
        fgets(buffer,100,f);
        
        //Pour chacune des lignes lues
        while(!feof(f)){
    
            //Extraction du type de transaction 2 cas: commande avec arguments et sans argument
            tok = strtok_r(buffer," .",&sp);
    
            //Branchement selon le type de transac  
            if(strcmp(tok,"AL") == 0){
                //Extraction des paramètres
                int noVersion = atoi(strtok_r(NULL," ",&sp));
                int noligne = atoi(strtok_r(NULL,&sp));
                char *tligne = strtok_r(NULL,"\n",&sp);
                //Appel de la fonction associée
                struct paramAL *ptr = (struct paramAL*) malloc(sizeof(struct paramAL));
                ptr->noVersion = noVersion;
                ptr->noligne = noligne;
                strcpy(ptr->tligne,(const char *)tligne);
                pthread_create(&tid[nbThread++],NULL,addItemL,(void*) ptr);   
            } else if(strcmp(tok,"CV") == 0) {
                //Extraction des paramètres
                int noVersion = atoi(strtok_r(NULL,"\n ",&sp));
                //Appel de la fonction associée
                copyItemV(VRAI,noVersion); 
            }
    
            //Lecture (tentative) de la prochaine ligne de texte
            fgets(buffer,f);
        }
        for(i=0; i<nbThread;i++)
            pthread_join(tid[i],NULL);
        //Fermeture du fichier
        fclose(f);
        //Retour
        return NULL;
    }

这是关注代码的一部分:

void* addItemL(void* parama){  
    char tl[100]; 
    int noVersion,nl;
    
    struct paramAL *param = parama;
        
    noVersion = param->noVersion;
    nl = param->noligne;
    strcpy(tl,(const char*)param->tligne);
    free(param);
    struct noeudV * ptrV;
    
    ptrV = findItemV(noVersion);

    // Verifier si la version existe
    if (ptrV==NULL)
        return NULL;

    //Création de l'enregistrement en mémoire
    struct noeudL* ni = (struct noeudL*)malloc(sizeof(struct noeudL));

    struct noeudL* ptrINS = findItemL(noVersion,nl);

    //Affectation des valeurs des champs
    ni->ligne.noligne   = nl;
    strcpy(ni->ligne.ptrligne,tl);

    if((ptrINS == NULL) && (nl == 1)) // ajout au debut de la liste vide
    {
        // premiere ligne premier noeud 
        ni->suivant= NULL;
        ptrV->finL = ptrV-> debutL = ni;

    }
    else if ((ptrINS == NULL) && (nl > 1)) // ajout a la fin de la liste
    {
        struct noeudL* tptr = ptrV->finL;
        ni->suivant= NULL;
        ptrV->finL = ni;
        tptr->suivant = ni;
    }
    else
    {
        struct noeudL* tptr = ptrINS;
        if(tptr == ptrV->debutL) // ajout a la tete de la liste
            ptrV->debutL = ni;
        else
        {
            struct noeudL* ptrPREV = findPrevL(noVersion,nl);
            ptrPREV->suivant = ni;
        }
        ni->suivant = tptr;
        
        while (tptr!=NULL) {
            //Est-ce le prédécesseur de l'item recherché?
            tptr->ligne.noligne++;
            //On retourne un pointeur sur l'item précédent

            //Déplacement du pointeur de navigation
            tptr=tptr->suivant;
        }
    }
    return NULL;
}

以下是valgrind检测到的错误

valgrind --leak-check=full ./gestionCVS_MAIN t2.txt                                                                                                                                                                            
==50153== Memcheck,a memory error detector                                                                                                                                                                                      
==50153== copyright (C) 2002-2013,and GNU GPL'd,by Julian Seward et al.                                                                                                                                                        
==50153== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info                                                                                                                                                    
==50153== Command: ./gestionCVS_MAIN t2.txt                                                                                                                                                                                      
==50153==                                                                                                                                                                                                                        
noVersion  Nom Version                                  
======= ================================================
1        V1

2        

3        

4        

======= ===============================================

noVersion noligne       texte                                          
=======   ===========   ==================================
=========================================================

noVersion noligne       texte                                          
=======   ===========   ==================================
=========================================================

noVersion noligne       texte                                          
=======   ===========   ==================================
=========================================================

==50153== Invalid read of size 4
==50153==    at 0x400E04: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==  Address 0x576f150 is 0 bytes inside a block of size 108 free'd
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==
==50153== Invalid read of size 4
==50153==    at 0x400E13: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==  Address 0x576f154 is 4 bytes inside a block of size 108 free'd
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==
==50153== Invalid read of size 1
==50153==    at 0x4C2E1C7: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E35: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==  Address 0x576f158 is 8 bytes inside a block of size 108 free'd
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==
==50153== Invalid read of size 1
==50153==    at 0x4C2E1E4: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E35: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==  Address 0x576f159 is 9 bytes inside a block of size 108 free'd
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==
==50153== Invalid free() / delete / delete[] / realloc()
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==  Address 0x576f150 is 0 bytes inside a block of size 108 free'd
==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
==50153==
noVersion  Nom Version                                  
======= ================================================
1        V1

2        

3        

4        

======= ===============================================

noVersion noligne       texte                                          
=======   ===========   ==================================
=========================================================

noVersion noligne       texte                                          
=======   ===========   ==================================
=========================================================

-rw-r--r-- 1 mi136 www-data  89 sep 29 21:16 V1.c
-rw-rw-r-- 1 mi136 www-data  44 oct 25 17:22 V1?.c
-rw-r--r-- 1 mi136 www-data 108 sep 29 21:16 V2.c
-rw-rw-r-- 1 mi136 www-data   0 oct 13 12:46 V2?.c

 Mon Premier Programme CVS

 Mon Premier programme CVS non pas le Dernier
==50153==
==50153== HEAP SUMMARY:
==50153==     in use at exit: 557,952 bytes in 4,104 blocks
==50153==   total heap usage: 8,215 allocs,4,113 frees,2,888,760 bytes allocated
==50153==
==50153== LEAK SUMMARY:
==50153==    definitely lost: 0 bytes in 0 blocks
==50153==    indirectly lost: 0 bytes in 0 blocks
==50153==      possibly lost: 0 bytes in 0 blocks
==50153==    still reachable: 557,104 blocks
==50153==         suppressed: 0 bytes in 0 blocks
==50153== Reachable blocks (those to which a pointer was found) are not shown.
==50153== To see them,rerun with: --leak-check=full --show-leak-kinds=all
==50153==
==50153== For counts of detected and suppressed errors,rerun with: -v
==50153== ERROR SUMMARY: 46 errors from 5 contexts (suppressed: 0 from 0)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。