locked
how to use VirtualAlloc on Win8 metro ?

    Question

  • hi all,

    i 've used VirtualAlloc for alloc memory and run ok on Win8 desktop, but when i migrate the code into Win8 metro, the function "VirtualAlloc()" is unsupported, and i try to use malloc instead of it,  then malloc cannot set the flag "PAGE_READWRITE", so failed, can anyone know how to do ?

    thanks in advance.

    Jackic


    one work one gain!

    Tuesday, January 8, 2013 9:57 AM

Answers

  • As you know, VirtualAlloc function is for desktop only and cannot be used in Windows Store apps.

    But you can use malloc or HeapAlloc to allocate memory from the process heap. HeapAlloc has less overhead but the allocated memory is not movable.

    PAGE_READWRITE flag is one of memory protection constants, which is used to enable read/write access to the committed region of pages. All these memory protection attributes cannot be assigned to a portion of a page. They can only be assigned to a whole page.

    The VirtualAlloc function allows you to specify additional options for memory allocation. It uses a page granularity for memory allocation and accordingly results in higher memory usage.

    But the allocation for either malloc or HeapAlloc is not based on page granularity. Naturally, PAGE_READWRITE flag cannot be used with either malloc or HeapAlloc function.


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, January 8, 2013 11:31 AM
    Moderator
  • malloc/new use HeapAlloc, and the memory allocated by HeapAlloc is by default "Read/Write".

    What you can't do in Windows Style apps is change memory protections. This means you cannot use PAGE_READONLY and more important you can't use PAGE_EXECUTE or PAGE_EXECUTEREAD. Since all Windows 8 and Windows RT machines have "NX" enabled, you cannot write self-modifying code or do a custom JIT in a Windows Store app.

    Tuesday, January 8, 2013 10:17 PM

All replies

  • As you know, VirtualAlloc function is for desktop only and cannot be used in Windows Store apps.

    But you can use malloc or HeapAlloc to allocate memory from the process heap. HeapAlloc has less overhead but the allocated memory is not movable.

    PAGE_READWRITE flag is one of memory protection constants, which is used to enable read/write access to the committed region of pages. All these memory protection attributes cannot be assigned to a portion of a page. They can only be assigned to a whole page.

    The VirtualAlloc function allows you to specify additional options for memory allocation. It uses a page granularity for memory allocation and accordingly results in higher memory usage.

    But the allocation for either malloc or HeapAlloc is not based on page granularity. Naturally, PAGE_READWRITE flag cannot be used with either malloc or HeapAlloc function.


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, January 8, 2013 11:31 AM
    Moderator
  • malloc/new use HeapAlloc, and the memory allocated by HeapAlloc is by default "Read/Write".

    What you can't do in Windows Style apps is change memory protections. This means you cannot use PAGE_READONLY and more important you can't use PAGE_EXECUTE or PAGE_EXECUTEREAD. Since all Windows 8 and Windows RT machines have "NX" enabled, you cannot write self-modifying code or do a custom JIT in a Windows Store app.

    Tuesday, January 8, 2013 10:17 PM
  • Thanks Robin_yang,

    i'll try to use malloc/free instead of VirtualAlloc/VirtualFree.

    Jackic


    one work one gain!

    Wednesday, January 23, 2013 9:42 AM