Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why do some people use HeapAlloc instead of malloc?

Why do some people use HeapAlloc instead of malloc?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chintoo723
    wrote on last edited by
    #1

    I have seen some folks using HeapAlloc instead of malloc, how is that different?

    thanks!

    B A C 3 Replies Last reply
    0
    • C Chintoo723

      I have seen some folks using HeapAlloc instead of malloc, how is that different?

      thanks!

      B Offline
      B Offline
      bob16972
      wrote on last edited by
      #2

      All my literature states they are basically the same. I guess portability would be the only difference. Malloc does not require a specific OS. HeapAlloc does. But correct me if I'm wrong...

      1 Reply Last reply
      0
      • C Chintoo723

        I have seen some folks using HeapAlloc instead of malloc, how is that different?

        thanks!

        A Offline
        A Offline
        Andy Moore
        wrote on last edited by
        #3

        They are effectively the same. However malloc is in the C runtime libraries, so if you use it your application has to link in the C runtime. HeapAlloc is defined in kernel32.dll, so if you want to reduce the code size of your application you can avoid linking to the C runtime and use HeapAlloc to allocate memory. Using HeapAlloc may make your code less portable if that is a concern for you. I hope this helps.

        Deus caritas est

        C 1 Reply Last reply
        0
        • A Andy Moore

          They are effectively the same. However malloc is in the C runtime libraries, so if you use it your application has to link in the C runtime. HeapAlloc is defined in kernel32.dll, so if you want to reduce the code size of your application you can avoid linking to the C runtime and use HeapAlloc to allocate memory. Using HeapAlloc may make your code less portable if that is a concern for you. I hope this helps.

          Deus caritas est

          C Offline
          C Offline
          Chintoo723
          wrote on last edited by
          #4

          Portability across different OSes or even across different flavours of Windows?

          thanks!

          A 1 Reply Last reply
          0
          • C Chintoo723

            Portability across different OSes or even across different flavours of Windows?

            thanks!

            A Offline
            A Offline
            Andy Moore
            wrote on last edited by
            #5

            Across different OSs.

            Deus caritas est

            1 Reply Last reply
            0
            • C Chintoo723

              I have seen some folks using HeapAlloc instead of malloc, how is that different?

              thanks!

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              Every process creates a default process heap. This is the heap used by malloc/new/... Aside from using the process heap, malloc/new/... (can) add other optimization features such as lookaside lists, small-block heaps, ... HeapAlloc is the low level way to alloc a block of memory on a heap. Some people will create a number of heaps within a program, each designed to hold a specific type of data or a specific group of data. This can help reduce heap fragmentation and speed up parts of some applications. I use separate heaps for different types of data (e.g. all string buffers in one heap, all blobs in another, ...). I use separate heaps for different groups of data (e.g. all database column data in one heap, all network packets in another, ...). This just touches on the subject. In the end it provides the developer with just another tool.

              ...cmk Save the whales - collect the whole set

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups