We all know that in the program can use malloc to allocate memory on the heap, obviously windows should allocate a piece of space for this heap, we use malloc in the main program to allocate a small block of memory to see where the pointer points:
char* p = (char*) malloc (10);
Get a pointer: 0x00b267b0
Looking inside the memory block, it's easy to find the target:
It can be seen from here that the algorithm used by malloc does not allocate a large amount of memory at the outset, if we proceed to:
p = (char*) malloc (0x10000);
Allocate a piece of 64K of memory, then you can find another piece of memory:
This memory is idle for the first time when it is allocated.
It can be inferred from this that the maximum amount of memory that malloc can allocate should depend on the largest free block. Write paragraph code to test:
void block_test()
{
SYSTEM_INFO info;
MEMORY_BASIC_INFORMATION mi;
HANDLE hProcess;
DWORD dwAddr;
MEMORY_BASIC_INFORMATION miBlock[1000];
int nCount = 0, nMaxSize = 0;
char* p = NULL;
hProcess = GetCurrentProcess();
GetSystemInfo(&info);
dwAddr = (DWORD)info.lpMinimumApplicationAddress;
do
{
VirtualQueryEx(hProcess, (LPCVOID)dwAddr, &mi, sizeof(mi));
memcpy(&miBlock[nCount++], &mi, sizeof(mi));
dwAddr += mi.RegionSize;
if((mi.State & MEM_FREE) && mi.RegionSize > nMaxSize)
nMaxSize = mi.RegionSize;
} while(dwAddr < (DWORD)info.lpMaximumApplicationAddress);
p = malloc(nMaxSize);
………..
}