Storage Allocation
-
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 ?
-
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 ?
There are differences between
HeapAlloc
andnew
(andmalloc
). See, for instance: Comparing Memory Allocation Methods (Windows)[^]. new operator - How do you 'realloc' in C++? - Stack Overflow[^]. c++ - malloc() vs. HeapAlloc() - Stack Overflow[^]. -
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 ?
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.
-
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.
-
The run time has it own heap I think the default heap When I create my heap I’m segragting the allocations
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.
-
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.
-
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
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.