Thread: VirtualAlloc() and free()?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    VirtualAlloc() and free()?

    Hi,
    I just saw something kind of scary in some C++ code I'm fixing. The code allocates memory with VirtualAlloc(), then then later deletes it using free().

    Is that safe?
    Should I change it to VirtualFree() instead?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is almost certainly not at all safe - in fact I'd be surprised if it actually works OK in the first instance.

    And yes, you should use free.

    If the original VirtualAlloc is a plain memory allocation with no "funny" parameters, it may not crash or be terribly bad in any other way, but you certainly wouldn't want any memory with "special" settings to end up in the heap.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks, I changed them to use VirtualFree().

Popular pages Recent additions subscribe to a feed