Thread: VirtualAlloc v.s. malloc

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    VirtualAlloc v.s. malloc

    Hello everyone,


    Any practical experiences of VirtualAlloc v.s. malloc? From MSDN, I can not find any comparisons between the two methods.


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    VirtualAlloc is the better choice if you want to fully control how your allocation is done, particularly if you allocate HUGE amounts of memory. For general allocations, it's better to use malloc - the reason being that VirtualAlloc is done in the kernel, malloc is part of the C-runtime (it will call VirtualAlloc or some such to get big chunks of memory that it then split up into smaller pieces).

    As you can see if you read the docs and understand about the memory mapping techology, the VirtualAlloc can not allocate small pieces of memory - the smallest "chunk" would be at least 4KB.

    Actually, malloc() uses "HeapAlloc" rather than VirtualAlloc. But VirtualAlloc is still best for LARGE chunks of memory, or when you have special requirements that aren't fulfilled by malloc() - for example the memory needs to be protected in special ways, you want to get exceptions when there is a write to the memory or some such.
    --
    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
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    VirtualAlloc is the better choice if you want to fully control how your allocation is done, particularly if you allocate HUGE amounts of memory. For general allocations, it's better to use malloc - the reason being that VirtualAlloc is done in the kernel, malloc is part of the C-runtime (it will call VirtualAlloc or some such to get big chunks of memory that it then split up into smaller pieces).

    As you can see if you read the docs and understand about the memory mapping techology, the VirtualAlloc can not allocate small pieces of memory - the smallest "chunk" would be at least 4KB.

    Actually, malloc() uses "HeapAlloc" rather than VirtualAlloc. But VirtualAlloc is still best for LARGE chunks of memory, or when you have special requirements that aren't fulfilled by malloc() - for example the memory needs to be protected in special ways, you want to get exceptions when there is a write to the memory or some such.
    --
    Mats
    What is the differences between HeapAlloc and VirtualAlloc? They allocate memory from different spaces?


    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    VirtualAlloc does two things. First, it reserves address space. Second, it commits memory to that address space. Those are very low-level operations. What VirtualAlloc doesn't do is keep the extensive management information that is required to make small allocations. It only does pagewise allocations.

    HeapAlloc keeps those data structures. HeapAlloc is pretty much the same as malloc, except that it is part of the Win32 API and not the CRT.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To use HeapAlloc you need to first do HeapCreate, to create a chunk of memory to allocate from. HeapAlloc is then used to "split" the chunk into smaller portions.

    As stated, VirtualAlloc is for reserving and/or committing a large chunk of memory - useful when you need megabytes. Committing here means that the memory map is filled in with ACTUAL memory pages [4KB portions], rather than just "this memory exists in the process space, but there's actually no real memory there" which is "reserving". If the memory isn't committed when it's being accessed, it will be comitted in the page-fault handler. This is handy if you want to reserve a really large chunk of memory, but you don't necessarily need to fill all parts of that memory with data [e.g. a HUGE hash-table, or a table indexed by some "random" numbers, telephone numbers or some such, where some ranges of numbers isn't actually used - so it's no point in actually taking up REAL memory for those positions].

    If you are doing regular programming, use malloc(). What I'm trying to say is, malloc() is a good average function. It's only when something malloc does is unsuitable for your application that you may need something else: Really big allocations, "sparse" allocations [don't commit memory unless it's actually used], special protection (run-time generated code, which requires VirtualProtect to set the execute-disable bit to zero for example - malloc can't do that).

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM