Thread: new invokes VirtualAlloc

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

    new invokes VirtualAlloc

    Hello everyone,


    On Windows, when we allocate large amount of memory, new will call VirtualAlloc directly, other than call HeapAlloc then through HeapAlloc call VirtualAlloc indirectly?

    Anyone to clarify this?


    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Implementation specific, once again.
    All libraries are not restricted to doing that since all the standard says AFAIK is that new allocates memory for C++.
    For Microsoft's implementation, new always calls HeapAlloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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


    Maybe my bad memory... :-)

    Quote Originally Posted by Elysia View Post
    Implementation specific, once again.
    All libraries are not restricted to doing that since all the standard says AFAIK is that new allocates memory for C++.
    For Microsoft's implementation, new always calls HeapAlloc.

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What difference does it make? The only thing that really matters is the whether the allocation succeeded or not, and that if it succeeds, that the returned memory address is available to the application.

    If you really want to know how it works, use WinDBG and put breakpoints on each of those entry points in a particular application. I expect VirtualAlloc is never called by new - it is pretty much meaningless to do so - it may of course happen during HeapAlloc, but in that case, it's probably the kernel-internal form of VirtualAlloc, rather than VirtualAlloc as a external function.

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

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


    I just want to learn how Windows optimize memory allocation according to the request size of memory to be allocated by asking this question -- the actual purpose is more about to get the idea other than get the real internal function to be called. I just want to learn this idea and improve my own engineering work.

    Quote Originally Posted by matsp View Post
    What difference does it make? The only thing that really matters is the whether the allocation succeeded or not, and that if it succeeds, that the returned memory address is available to the application.

    If you really want to know how it works, use WinDBG and put breakpoints on each of those entry points in a particular application. I expect VirtualAlloc is never called by new - it is pretty much meaningless to do so - it may of course happen during HeapAlloc, but in that case, it's probably the kernel-internal form of VirtualAlloc, rather than VirtualAlloc as a external function.

    --
    Mats

    regards,
    George

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For 99.9% of all memory allocations, HeapAlloc is fine. If you are working with special memory (such as in device drivers or applications that talk directly to lower level device drivers), you may find that VirtualAlloc is meaningfull - I may have stated earlier that VirtualAlloc isn't for small allocations, but it's not really a suitable replacement for HeapAlloc for large allocations either. Pretty much the answer is "If you don't know for sure that you need VirtualAlloc, you probably don't".

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

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your comments, Mats!


    Quote Originally Posted by matsp View Post
    For 99.9% of all memory allocations, HeapAlloc is fine. If you are working with special memory (such as in device drivers or applications that talk directly to lower level device drivers), you may find that VirtualAlloc is meaningfull - I may have stated earlier that VirtualAlloc isn't for small allocations, but it's not really a suitable replacement for HeapAlloc for large allocations either. Pretty much the answer is "If you don't know for sure that you need VirtualAlloc, you probably don't".

    --
    Mats

    regards,
    George

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only advantage of VirtualAlloc that I know of is that you can reserve and commit specific virtual memory areas, making it perfect for expanding arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, and access privilege. :-)


    Quote Originally Posted by Elysia View Post
    The only advantage of VirtualAlloc that I know of is that you can reserve and commit specific virtual memory areas, making it perfect for expanding arrays.

    regards,
    George

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But that is usually not a very good advantage, since it would rarely be used I think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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


    My question is answered.

    Quote Originally Posted by Elysia View Post
    But that is usually not a very good advantage, since it would rarely be used I think.

    regards,
    George

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    All of the usages of VirtualAlloc are rare - that was my point. Expanding arrays is fine, but you do need to know the maximum size of the array, and once you have reserved the memory, you can't use it for anything else.

    Setting access privileges is even rarer - I have seen code that does that, but it's not exactly mainstream code.

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

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for sharing experience Mats,


    Quote Originally Posted by matsp View Post
    All of the usages of VirtualAlloc are rare - that was my point. Expanding arrays is fine, but you do need to know the maximum size of the array, and once you have reserved the memory, you can't use it for anything else.

    Setting access privileges is even rarer - I have seen code that does that, but it's not exactly mainstream code.

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-10-2009, 01:55 PM
  2. VirtualAlloc() and free()?
    By cpjust in forum Windows Programming
    Replies: 2
    Last Post: 01-21-2008, 10:55 AM
  3. new and VirtualAlloc
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 01-13-2008, 09:13 AM
  4. VirtualAlloc help
    By Elysia in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2007, 09:32 AM
  5. VirtualAlloc v.s. malloc
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 07:50 AM