XP User Program space allocation (7): Heap

Source: Internet
Author: User

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);
………..

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.