If I were to want to allocate memory for strings or perhaps lots of small little structres say only a couple of dwords in size what would be the preferable method.

I'd be thinking of something like GlobalAlloc or HeapAlloc. Of course with HeapAlloc you need a Heap, would you be better off creating your own or using the Process Heap?

Another option could be to allocate a big chunk of memory then manage it yourself, but that would be tricky.

The easy way out is obviously GlobalAlloc, f0dder always said he preferred HeapAlloc and it would be just as easy I suppose. Anyone ever given this thought before?
Posted on 2002-12-24 17:51:12 by Eóin
I use a lot of GlobalAlloc for small stuff as you mension but if i did something big i would use HeapAlloc or learn about the other ALLoc that f0dder try to teach us about.

Other than that if i could find the proper way i would just make a buffer in the app that can do it all... I figure WHY NOT ... It is said (**f0dder**) that Windows gives every app 1 meg of memory if we use it or not. So I plan to learn how to use every bit of it get rid of Global and call Heap if ever needed even though Global is my favorite. (maybe off key, just my thought)
Posted on 2002-12-24 18:43:26 by cmax
Allocating small chunks (even several bytes in size) will end up allocating at least one page minimum, which is 4096 bytes in size. I suggest you implement own allocation system, and resize allocated buffer when necessary.
Posted on 2002-12-24 19:42:02 by comrade
This discussion has been done before, if you want a lot of small string allocations, use OLE string or perhaps GlobalAlloc as they have much finer granularity than the heap based techniques.

Otherwise, manage you own out of a single large block and maintain pointers to it.

Regards,

hutch@movsd.com
Posted on 2002-12-24 20:33:04 by hutch--
blah blah, people not knowing what theyre sayiing. localalloc=globalalloc=heapalloc. Fine for allocs <=256MB. Or at least that's what it works with. You might want to try your own schemes, if it matters. If you have really heavy requirekents, the nature of virtualalloc might help you - but you will probably have either very advanced requirements of >= 256MEG alloc chunks before you need this
Posted on 2002-12-24 20:47:42 by f0dder
If you'll choose heap solution, and you structures have fixed, but different size, it's much more efficient to use individual heap for every chunk of structures.

If you mix structures with different size in one heap, it's get fragmented, and will grow.
Posted on 2002-12-25 05:08:30 by Four-F
If I were to want to allocate memory for strings or perhaps lots of small little structres say only a couple of dwords in size what would be the preferable method.
If I understand correctly what you mean, you will need a pointer anyway for each of your allocated memory block. A pointer is 4 bytes, a block (given your scenario) is on average a bit more than that.
I'd certainly opt to change the design radically. In any case, write your own allocator.
Posted on 2002-12-25 05:25:16 by Maverick
Thanks everyone for your replies. I understand now that writing my own allocator is probably the ideal solution.

But can I as a final question on HeapAlloc, Lets say I want ten 64 byte structre. Theres the obvious way of allocating 640 bytes and using offsets into the memory. But if one were to do things the silly way and alocate 10 seperate 64 byte chunks how much additional (approx) memory is wasted. As Marerick points out theres the ten 4 byte pointers as opposed to one, but is extra memory also wasted, due to alignment perhaps.

I the past the only function I saw returning alligned memory was virtualalloc.
Posted on 2002-12-26 10:02:19 by Eóin

If you'll choose heap solution, and you structures have fixed, but different size, it's much more efficient to use individual heap for every chunk of structures.

If you mix structures with different size in one heap, it's get fragmented, and will grow.


Just a quick note. If you need to make hundreds, or thousands of small allocations, you may have problems when your app exits, if you use (Global)HeapAlloc and call (Global)HeapFree for every one of the allocations.

We worked with a 3D engine API that did just that. It would take along time for Windows to free all the memory. Finally we just used KillProcess to exit from the app. This was targeted for a game console. Only for our internal editor was the KillProcess hack used.
Posted on 2002-12-26 10:48:35 by ThoughtCriminal
Thats sounds important to know, thanks ThoughtCriminal.
Posted on 2002-12-26 12:50:45 by Eóin