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