VirtuallAlloc and malloc

This is a discussion on VirtuallAlloc and malloc within the C Programming forums, part of the General Programming Boards category; Is Code: malloc(size) equals to Code: VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE) ? So do Code: VirtualFree(memblock, size, MEM_DECOMMIT) equals to Code: ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Question VirtuallAlloc and malloc

    Is
    Code:
    malloc(size)
    equals to
    Code:
    VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
    ?

    So do
    Code:
    VirtualFree(memblock, size, MEM_DECOMMIT)
    equals to
    Code:
    free(memblock)
    ?

    Btw, I want to modify TCC (TinyCC) and make it free from msvcrt.dll ^_^

    Borland's and DigitalMars C compiler are free from msvcrt but unfortunately they produce BIG execute-able file.

    Anybody tried TCC (TinyCC)?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    When I look at the CRT source code for malloc() in Visual Studio, it looks like it's calling HeapAlloc().

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Quote Originally Posted by cpjust View Post
    When I look at the CRT source code for malloc() in Visual Studio, it looks like it's calling HeapAlloc().
    What is the different between heap and virtual memory?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    malloc and VirtualAlloc are not the same thing. You cannot depend on the knowledge that they are because each implementation is different from the other. Malloc is not guaranteed to call VirtualAlloc. It can call HeapAlloc or something else.

    VirtualAlloc has a different purpose than malloc, and should be used sparingly.

    And I recommend you don't free the executable from the dll. If you do, that code needs to be inside the executable, thus bloating the exe instead.
    See it this way: you only need to copy that dll once, and never more. But if you don't do that and include the code in the exe, you will have to download extra each time.

    Static vs. dynamic.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    I don't know msvcrt.dll's license... T_T

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    It's a runtime dll. You can redistribute it to any computer you please.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21