The simplest implementation is one of the famous K & RC Bible , p. 186 - 188.
The memory block that we actually get is larger (the structure head or the size of the union head) than we apply. The structure may be as follows:
typedef long Align; union header { struct { union header* ptr;
Figure to demonstrate this:

When we call the free function, the behavior may be like this:
void free(void* ptr) { Header *bp, *p; bp = (Header *)ptr - 1; }
In the visual studio, we have two models: release version and debug version , we could even use the head to store a debug message to facilitate debugging. The header in the debug version is called _CrtMemBlockHeader , the definition is as follows:
typedef struct _CrtMemBlockHeader { struct _CrtMemBlockHeader * pBlockHeaderNext; struct _CrtMemBlockHeader * pBlockHeaderPrev; char * szFileName; int nLine; size_t nDataSize; int nBlockUse; long lRequest; unsigned char gap[nNoMansLandSize]; } _CrtMemBlockHeader;
Then lalout memory:

source share