Exploration about ‘HeapCreate’ s second parameter.

#include "stdafx.h"
#include <windows.h>
#include <Tlhelp32.h>
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    //HANDLE WINAPI HeapCreate(
    //  _In_  DWORD flOptions,
    //  _In_  SIZE_T dwInitialSize,                          // This parameter is used for optimazation. 
    //                                                       // It ensures there will be a region which is equal or large than 'dwInitialSize'.
    //  _In_  SIZE_T dwMaximumSize
    //);
    HANDLE hHeap = HeapCreate(0, 1024 * 5, 1024 * 8);        // Maximum 1Mb. There is a region with size 1024*5.

    {
        PROCESS_HEAP_ENTRY phe;
        phe.lpData = nullptr;
        cout <<"There must be a region larged than 1024*5." <<endl;
        while (HeapWalk(hHeap, &phe))
        {
            cout <<phe.cbData <<endl;                        // Heap layout is "1176, 6968, 0".
        }
        cout <<"-" <<endl;
    }

    {
         char *data = static_cast<char *>(HeapAlloc(hHeap, 0, 1024 * 2));    // But we need a 1024 * 2. Region "6968" has to be splitted.

        PROCESS_HEAP_ENTRY phe;
        phe.lpData = nullptr;
        cout <<"The region '6968' is splitted into '2048' & '4912'." <<endl;
        while (HeapWalk(hHeap, &phe))
        {
            cout <<phe.cbData <<endl;
        }
        cout <<"-" <<endl;

        HeapFree(hHeap, 0, data);
        data = nullptr;
    }

    HeapDestroy(hHeap);
    return 0;
}

 
posted @ 2012-10-15 14:13  walfud  阅读(215)  评论(0编辑  收藏  举报