The way of C / C + + Learning

Time:2021-6-6
First of all, explain the problem of heap corruption deleted. The compiler reported an error mainly due to improper memory new / delete. If the compiler prompts heap corruption deleted and CRT detected that the application write to memory after end of Heap Buffer, the following aspects should be checked first:
1) Whether the space of new is enough, and whether some operations in the middle make the array out of bounds.
2) Is the space released by delect your new space? Is there any more or random release.
3) Is it true that the space has been delected.
Secondly, let's explain what heap Corporation is. When the space we use exceeds the pre applied space, the extra part will cover the memory lower bound identifier and the subsequent storage space, which is called heap corruption. If the important data is stored in the space of heap corruption, hackers can use heap corruption to modify the data.
After explaining the problem of heap allocation, let's study the implementation principle of heap allocation in memory. If we allocate 10 bytes of space, then we need about 32 bytes of additional space to store its management information. In heap, OS uses bidirectional linked list structure to manage memory. In normal mode, OS uses double linked list structure to manage information_ The crtmemblockheader structure is saved. In this structure, the front pointer pblockheaderprev and the back pointer pblockheadernext are defined. With these two pointers, all the memory space applied can be traversed. In this structure, the member variable Irequest is used to record how many times the current heap has been applied. Heap release operation is: according to the first address of the heap, the released heap is delisted from the linked list to complete the heap release operation. At the end of the heap data is the cross boundary check flag of 0xfdfd. Once this flag is covered, the error of heap collision will be reported.
To sum up, when new is a memory, the system will use it_ Crtmemblockheader, a bidirectional linked list, manages the heap space. This structure stores the addresses of the upper and next heap, the size of this heap allocation, and the cross-border identifier of the heap. Debug will check this structure to determine whether the memory allocation and release are correct. In case of cross-border, it will report a message of heap corruption, Then it is very possible to eliminate errors by focusing on the reasons mentioned above.