Using DevPartner's blockchecker to check for C + + memory errors

Source: Internet
Author: User

In the process of copying STL, it was a memory error card for a long time. Heap corruption errors occur when the memory pool requires multiple malloc, and the initial judgment is that the array is out of bounds, but it is always checked out. Start with dr.memory check not come out, try a bit devpartner. Website Link Http://www.borland.com/zh-CN/Products/Software-Testing/Automated-Testing/Devpartner-Studio

DevPartner installs the plugin automatically at VS, but that plugin doesn't seem to work. I am directly in the installation directory to find Blockchecker, and use it to open the EXE to debug, and then click Run, you can see the detected errors,

It suggests that the pointer operation range is wrong, why is it wrong? You can see the code above

    *my_free_list = next = (obj *) chunk + N;

should be for

    *my_free_list = next = (obj *) (chunk + N);

The original is to forget to add brackets!! Because the obj-type pointer is not the same size as the char pointer, the result of adding to n is naturally different. A parenthesis wasted so much of my time ... So how important it is to have a good tool. But no good tool can clearly point out where it went wrong, like the example above, and the last step is to launch it ourselves.

1 //returns an object of size n, and may join other chunks of size n to freelist2 //in ANSI C, void * Does not allow the addition and subtraction of operations, so chunk with char *3 void*Default_alloc::refill (size_t N)4 {5     intOBJS = -;6     Char*chunk =chunk_alloc (n, OBJS);7     8obj *next = nullptr, *current =nullptr;9obj *result =nullptr;Tenobj **my_free_list =nullptr; One     if(1= = OBJS)//remove only one chunk A         returnChunk; -My_free_list = Free_list +Freelist_index (n); -result = (obj *) chunk;//this piece is returned to the client the     //Point Freellist to the assigned area -*my_free_list = next = (obj *) (chunk +n); -      for(inti =1;; i++) -     { +Current =Next; -Next = (obj *) (Char*) next + N);//Note that you can't use next+n directly here . +         if(i = = Objs-1) A         { atCurrent->next =nullptr; -              Break; -         } -         Else -Current->next =Next; -     } in     returnresult;  -}
View Code

Use DevPartner Blockchecker to check for C + + memory errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.