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

Storage Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I have some questions regarding storage allocations. I know there is default heap value every exe or DLL gets. You can make that bigger or smaller /HEAP I am guessing this is where the "new" operator gets storage. using GetProcessHeap gets storage from this heap If I don't want to bump heads with that I do a HeapAlloc initially the three paramters all zeros But I "THINK" even if I do a HeapAlloc for 20 byes it really gets 1K if I see that during my processing I need more I do a HeapRealloc using the same pointer for return value and 1st parameter I get more storage I don't know if this better than using new would anyone have any input ?

    CPalliniC J 2 Replies Last reply
    0
    • F ForNow

      Hi I have some questions regarding storage allocations. I know there is default heap value every exe or DLL gets. You can make that bigger or smaller /HEAP I am guessing this is where the "new" operator gets storage. using GetProcessHeap gets storage from this heap If I don't want to bump heads with that I do a HeapAlloc initially the three paramters all zeros But I "THINK" even if I do a HeapAlloc for 20 byes it really gets 1K if I see that during my processing I need more I do a HeapRealloc using the same pointer for return value and 1st parameter I get more storage I don't know if this better than using new would anyone have any input ?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      There are differences between HeapAlloc and new (and malloc). See, for instance: Comparing Memory Allocation Methods (Windows)[^]. new operator - How do you 'realloc' in C++? - Stack Overflow[^]. c++ - malloc() vs. HeapAlloc() - Stack Overflow[^].

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • F ForNow

        Hi I have some questions regarding storage allocations. I know there is default heap value every exe or DLL gets. You can make that bigger or smaller /HEAP I am guessing this is where the "new" operator gets storage. using GetProcessHeap gets storage from this heap If I don't want to bump heads with that I do a HeapAlloc initially the three paramters all zeros But I "THINK" even if I do a HeapAlloc for 20 byes it really gets 1K if I see that during my processing I need more I do a HeapRealloc using the same pointer for return value and 1st parameter I get more storage I don't know if this better than using new would anyone have any input ?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        ForNow wrote:

        But I "THINK" even if I do a HeapAlloc for 20 byes it really gets 1K I don't know if this better than using new

        There is a very specific reason for using HeapAlloc and nothing you posted suggests you need that. And not a good idea to attempt to circumvent C++ compiler allocations unless you have identified a specific need and you understand exactly how allocations work. If you have some limited need or just want to mess around then you can override allocations for a specific class (one class only) and control it in fine detail. And do so knowing that you are not as likely to mess up the rest of the application doing it. Alternatively find several open source allocators and heaps and examine how they are implemented first to understand what happens with heaps. Keep in mind that if you do attempt to control your own allocations you can completely destroy the application (runtime) because you can end up overwriting memory that has nothing to do with what you think it should. So do it very carefully.

        F 1 Reply Last reply
        0
        • J jschell

          ForNow wrote:

          But I "THINK" even if I do a HeapAlloc for 20 byes it really gets 1K I don't know if this better than using new

          There is a very specific reason for using HeapAlloc and nothing you posted suggests you need that. And not a good idea to attempt to circumvent C++ compiler allocations unless you have identified a specific need and you understand exactly how allocations work. If you have some limited need or just want to mess around then you can override allocations for a specific class (one class only) and control it in fine detail. And do so knowing that you are not as likely to mess up the rest of the application doing it. Alternatively find several open source allocators and heaps and examine how they are implemented first to understand what happens with heaps. Keep in mind that if you do attempt to control your own allocations you can completely destroy the application (runtime) because you can end up overwriting memory that has nothing to do with what you think it should. So do it very carefully.

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          The run time has it own heap I think the default heap When I create my heap I’m segragting the allocations

          J 1 Reply Last reply
          0
          • F ForNow

            The run time has it own heap I think the default heap When I create my heap I’m segragting the allocations

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            ForNow wrote:

            The run time has it own heap I think the default heap

            Modern OSes generally have at least two allocators. So in C++ the OS provides space to the app and then C++ itself manages that heap with its own allocator. Same is true for Java and C#.

            ForNow wrote:

            When I create my heap I’m segragting the allocations

            Not sure that what that means. An heap already represents a segregation. The C++ heap (every one I have looked at going back decades) dynamically manages blocks that it requested from the OS. Then on top of that it lays down a simple (or even complex) traditional heap. Then when you use 'new' (presuming you do not otherwise provide an allocator) it uses that existing API to request an amount of space appropriate for usage. Generally the allocation is exact to the structure. The structure might have padding but that is intrinsic to the structure and not the allocation.

            F 1 Reply Last reply
            0
            • J jschell

              ForNow wrote:

              The run time has it own heap I think the default heap

              Modern OSes generally have at least two allocators. So in C++ the OS provides space to the app and then C++ itself manages that heap with its own allocator. Same is true for Java and C#.

              ForNow wrote:

              When I create my heap I’m segragting the allocations

              Not sure that what that means. An heap already represents a segregation. The C++ heap (every one I have looked at going back decades) dynamically manages blocks that it requested from the OS. Then on top of that it lays down a simple (or even complex) traditional heap. Then when you use 'new' (presuming you do not otherwise provide an allocator) it uses that existing API to request an amount of space appropriate for usage. Generally the allocation is exact to the structure. The structure might have padding but that is intrinsic to the structure and not the allocation.

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              New works well when you now Exactly howvmuch storage you want I.e a object or class In my case I am capturing user input and not quite sure thus HeapReAlloc

              J 1 Reply Last reply
              0
              • F ForNow

                New works well when you now Exactly howvmuch storage you want I.e a object or class In my case I am capturing user input and not quite sure thus HeapReAlloc

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                ForNow wrote:

                New works well when you now Exactly howvmuch storage you want I.e a object or class

                That is not true. For an object in C++ you must ALWAYS allocate at least as much space is needed for the object. You can allocate more but it will be wasted. One can force C++ to use space that is less but that would be nothing but bad programming.

                ForNow wrote:

                In my case I am capturing user input and not quite sure thus HeapReAlloc

                All that means is that you are dynamically allocating the space. Which is something that C++ allows directly. You do not need to use a system API method to do that. As I previously pointed out HeapReAlloc has a specific purpose. And what you just described here is an inappropriate use of that. And one that could lead to resource (memory) starvation if you are not careful how you use it.

                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