Thread: new and VirtualAlloc

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

    new and VirtualAlloc

    Hello everyone,


    I heard some points that the memory allocated by new does not belongs to virtual memory, and onlymemory allocated by VirtualAlloc belongs to virtual memory.

    1.

    I think this statement is not correct. Since all memory touched by current process belongs to virtual memory (either reserved or committed). No matter how (through which API) developer is using.

    2.

    And new always utilize VirtualAlloc in underlying internal implementation code, so new (in implementation point of view) will consume virtual memory -- the same as what VirtualAlloc does.

    Are my points (1) and (2) correct?


    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    I heard some points that the memory allocated by new does not belongs to virtual memory, and onlymemory allocated by VirtualAlloc belongs to virtual memory.

    1.

    I think this statement is not correct. Since all memory touched by current process belongs to virtual memory (either reserved or committed). No matter how (through which API) developer is using.
    This is false. All memory you allocate is access within your virtual memory, whether it be new, malloc, VirtualAlloc, HeapAlloc, or whatever.

    2.

    And new always utilize VirtualAlloc in underlying internal implementation code, so new (in implementation point of view) will consume virtual memory -- the same as what VirtualAlloc does.

    Are my points (1) and (2) correct?
    No, that's false. This I would believe is implementation defined, but I can tell you that Microsoft's implementation uses HeapAlloc and not VirtualAlloc.
    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,


    Quote Originally Posted by Elysia View Post
    No, that's false. This I would believe is implementation defined, but I can tell you that Microsoft's implementation uses HeapAlloc and not VirtualAlloc.
    1. I think you mean new is using HeapAlloc, right?

    2. HeapAlloc is using VirtualAlloc? Or as a standalone implementation?


    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    1. I think you mean new is using HeapAlloc, right?
    As I mentioned, every vendor is free to implement the CRT and STL as they want as long as the result is the same. Thus some may use VirtualAlloc, some may use HeapAlloc. It's up to the vendor.
    Microsoft is using HeapAlloc.

    2. HeapAlloc is using VirtualAlloc? Or as a standalone implementation?
    They are stand along implementation because they do not do the same thing and do not work the same way.
    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.

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


    It is my mistake that I think HeapAlloc is always based on VirtualAlloc. Now from your description, I understand they have no dependent relationship.

    Could you provide some more information about what do you mean "do not do the same thing and do not work the same way"?

    Quote Originally Posted by Elysia View Post
    As I mentioned, every vendor is free to implement the CRT and STL as they want as long as the result is the same. Thus some may use VirtualAlloc, some may use HeapAlloc. It's up to the vendor.
    Microsoft is using HeapAlloc.


    They are stand along implementation because they do not do the same thing and do not work the same way.

    regards,
    George

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe you should study what a heap is. In Windows, you create several heaps, some private, as well. HeapAlloc creates a heap.
    VirtualAlloc on the other hand, simply reserves or commits pages within your virtual memory space.
    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.

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


    I used them before. I remembered some time before, I saw a architecture figure which describes all memory on Windows are based on virtual memory. So, I thought HeapAlloc is just an exported interface to provide application with heap perspective operation, and internally still based on VirtualAlloc (dealing with raw pages). :-)

    Quote Originally Posted by Elysia View Post
    Maybe you should study what a heap is. In Windows, you create several heaps, some private, as well. HeapAlloc creates a heap.
    VirtualAlloc on the other hand, simply reserves or commits pages within your virtual memory space.

    regards,
    George

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even if they are, the API has two different purposes, so they should not be treated the same. Even if they wrap each other in the internal implementation, it doesn't matter to us.
    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
    Thanks Elysia,



    Quote Originally Posted by Elysia View Post
    Even if they are, the API has two different purposes, so they should not be treated the same. Even if they wrap each other in the internal implementation, it doesn't matter to us.
    Back to my original question, I think my question is answered with your patience help. :-)

    I want to confirm with you that the answer is, the space allocatedby either new (memory on heap) or VirtualAlloc (memory on virtual memory page directly) is counted as part of virtual memory, right?


    regards,
    George

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, both are virtual memory.
    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
    Thanks Elysia,


    I appreciate your patience again on this thread. :-)

    Quote Originally Posted by Elysia View Post
    Yes, both are virtual memory.

    regards,
    George

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In the end, everything comes down to the memory API of VirtualAlloc. This includes CreateHeap/HeapAlloc.

    The reason is that the Win32 subsystem (ntdll.dll) provides no other way of getting memory from the OS.
    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

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your final conclusion, CornedBee!


    Quote Originally Posted by CornedBee View Post
    In the end, everything comes down to the memory API of VirtualAlloc. This includes CreateHeap/HeapAlloc.

    The reason is that the Win32 subsystem (ntdll.dll) provides no other way of getting memory from the OS.

    have a good weekend,
    George

Popular pages Recent additions subscribe to a feed