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. best way to allocate memory?

best way to allocate memory?

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
4 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.
  • N Offline
    N Offline
    nm_114
    wrote on last edited by
    #1

    Are any of the following ways of allocating memory faster or more efficient than the others: malloc, calloc, free c++ new, delete LocalAlloc, LocalFree GlobalAlloc, GlobalFree CoTaskMemAlloc, CoTaskMemFree -thanks

    J G P 3 Replies Last reply
    0
    • N nm_114

      Are any of the following ways of allocating memory faster or more efficient than the others: malloc, calloc, free c++ new, delete LocalAlloc, LocalFree GlobalAlloc, GlobalFree CoTaskMemAlloc, CoTaskMemFree -thanks

      J Offline
      J Offline
      Johnny
      wrote on last edited by
      #2

      Stick with 'new' and 'delete', unless you have any specific requirements to use the other methods.

      1 Reply Last reply
      0
      • N nm_114

        Are any of the following ways of allocating memory faster or more efficient than the others: malloc, calloc, free c++ new, delete LocalAlloc, LocalFree GlobalAlloc, GlobalFree CoTaskMemAlloc, CoTaskMemFree -thanks

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        malloc and free are probably the fastest, given that in most heap managers, they are obtaining memory from a pool managed by the C runtime. calloc is inherently slower than malloc, since it calls malloc and then initializes the allocated memory to all zero's. new and delete are slower than malloc and free, since they allocate/deallocate memory and then call the constructor/destructor for the object. The remaining functions I would think would be slower than malloc/free/new/delete, since they involve Win32 API or COM calls. That said, I agree with the other poster. If you are using C++, you should use new and delete wherever possible. Most of the time, the first thing you do with allocated memory is initialize it. The C++ new operator couples the allocation and initialization together, making it more difficult to fail to initialize what your allocating.


        Software Zen: delete this;

        1 Reply Last reply
        0
        • N nm_114

          Are any of the following ways of allocating memory faster or more efficient than the others: malloc, calloc, free c++ new, delete LocalAlloc, LocalFree GlobalAlloc, GlobalFree CoTaskMemAlloc, CoTaskMemFree -thanks

          P Offline
          P Offline
          Paul Ranson
          wrote on last edited by
          #4

          With the MS C/C++ library running on W2K etc then I think you'll find that all those end up at the API 'HeapAlloc'. So if you're really after most efficiency then consider going direct. In general though 'new'/'delete' for C++. And create your own allocators for special cases. For instance if you're allocating large numbers of small constant sized objects then a specialised allocator based on a number of larger allocations from the system is worth looking at. As in all optimisation the big win is to get the high level aspects of the program efficient before micro optimising. Say you make all your allocations run in 95% of the time, and I tweak my app to do half the number of allocations. Who's won? Who's kept their app maintainable and simple? Etc etc. Paul

          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