Menu

How to deal with mismatchAllocDealloc

2016-04-18
2016-04-20
  • Eckard Klotz

    Eckard Klotz - 2016-04-18

    Hello Everybody.

    I get the following CppCheck output with the code-fragment below:

    ....\src\TxtDiAsm\Parser\Data\TiPa_Filter.cpp|216|mismatchAllocDealloc : error : Mismatching allocation and deallocation: Word|

     set<int>* SpecialNodes = nullptr;
     switch(nodeType)
     // SpecialNodes should point to an object-attribute
     {
      case TiPa_Filter::NodesToKeep: SpecialNodes = &(Att.KeepNodes); break;
      case TiPa_Filter::NodesToSkip: SpecialNodes = &(Att.SkipNodes); break;
      case TiPa_Filter::NodesAsLeaf: SpecialNodes = &(Att.LeafNodes); break;
      default:                       SpecialNodes = nullptr;          break;
     }
    
     if(!(SpecialNodes->empty()))
      SpecialNodes->clear();
    
     if((separatedList != nullptr)&&(strlen(separatedList)>0)) 
     {
      int          Size  = strlen(separatedList);           
      const char*  Begin = separatedList;                   
      const char*  End   = strchr(separatedList,separator); 
      char*        Word  = new char[Size+1];      
         // allocate memmory for Word           
    
      if(End==nullptr)                                    
       End = separatedList + Size;                 
    
      while(  (End   != nullptr)
            &&(Begin <  separatedList + Size)
           )                                       
      {
        while(  (Begin[0] <'0')
              &&(Begin[0] >'9')
              &&(Begin    <  End)
             )                                     
         ++Begin;                                 
    
       strncpy(Word,Begin,End-Begin);           
            // put data into word        
       Word[End-Begin] = 0;                     
           // terminate string  
    
       SpecialNodes->insert(atoi(Word));        
          // transfer word into number         
       Begin = End + 1;                            
       End   = strchr(Begin,separator);            
       if(End==nullptr)                             
        End = separatedList + Size;                
      }
      delete(Word);                            
         // free the memomry of word 
     }
     else
     {
    
     }
    

    What is my mistake?

    Best regards,
    Eckard Klotz.

     

    Last edit: Eckard Klotz 2016-04-18
  • prozak

    prozak - 2016-04-19

    delete [] Word ?

     
  • Eckard Klotz

    Eckard Klotz - 2016-04-19

    Hello Prozak.

    Sorry but your comment is a little bit to short to be understanable for me.
    I guess you want to tell me that I try to delete an array. But is this realy the case if I write

    char*        Word  = new char[Size+1];
    

    Until now I thought that word is a pointer and with new I create an array in the heap. Thus my understanding is that I have to delete it again to free the allocated memmory.

    Could you please extend your comment to explain what you mean?

    Thank you very much,
    Eckard Klotz.

     

    Last edit: Eckard Klotz 2016-04-19
  • prozak

    prozak - 2016-04-20

    Hi Eckard,

    if you allocate memory for array using new [] operator, it should be deleted with delete [] operator.

    For more information please see:
    http://www.cplusplus.com/reference/new/operator%20delete[]/

    Regards,
    prozak

     
    • Eckard Klotz

      Eckard Klotz - 2016-04-20

      Hello Prozak.

      Yes now I understand. Thanks for your explanation and the link.

      Best regards,
      Eckard.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.