Phil CK

Virtual Memory

Last updated on

Virtual memory has some pretty cool tricks, however what are the basics of just using it.

/* valloc.c */

#include <Windows.h>

struct thing {
        int i;
};

int
main() {

        /* allocates the addresses but not the memory */
        LPVOID addr = VirtualAlloc(
                NULL,
                sizeof(struct thing) * 1000000000,
                MEM_RESERVE,
                PAGE_NOACCESS);

        /* we need the page size */
        SYSTEM_INFO sys_info;
        GetSystemInfo(&sys_info);


        /* reserve the memory, but it doesn't allocate until write,
           this allocates 2 pages of memory */
        addr = VirtualAlloc(
                addr,
                sys_info.dwPageSize * 2,
                MEM_COMMIT,
                PAGE_EXECUTE_READWRITE);

        /* cast the pointer then write */
        struct thing *th_array = (struct thing*)addr;

        th_array[0].i = 123;

        return 0;
}

Care must be taken that you only write within the pages you have allocated in the second call.

MSDN:VirtualAlloc