C/C++ thoughts on malloc/free/new/delete

The allocated space size needs to be specified when using malloc and new, but why do you only need to pass in a pointer when using free and delete? How to obtain the pointer space length during memory release?

Conclusion: Simply put, it can be understood that when allocating space, there are data headers and data segments. The pointer we apply for space allocated to us points to the position after the data header and before the data segment. When releasing, the memory is released according to the length recorded in the data header.

Insert image description here

Reference:
Reference 1: What is the size of the free space?
Reference 2: Delete length and size.
Reference 3: The underlying implementation of malloc, ptmalloc.

Guess you like

Origin blog.csdn.net/only_a_Heroic_car/article/details/128497409