Iv'e been reading in API reference about GlobaAlloc And LocalAlloc

Now i know they are the same under win32 but why would one use them. Is it only for when your writing files etc. ? In what situations would you use them? The reason i ask is because my program uses a lot of bitmaps as resources. Should i be allocating memory for them or does it matter?

Newbie here

Thanx in advance

Posted on 2001-10-13 21:36:40 by titan
One would use runtime memory allocation when temporary storage memory is needed.
Suppose you must open a configuration file and process it.
And let's suppose also that the configuration file is a variable-length one. It could contain strings from 0 to 255 bytes. (this is an example). How will you (when writting your program's source code) how much the conf. file will weight? how can you know in advance wether the file will be 1 or 20 kbytes long?
You can't know in advance. Unless you are nostradamus or some kind of wizzard. hehe. So the way to solve this problems is to allocate memory at runtime.
If you are dealing with FIXED size data then you don't need to allocate.(in most cases).
Anyway, it also depends on the functionality of your program.
Check Iczelion's tutorial on memory allocation for a nice example.
Hope this helps in any way.

Posted on 2001-10-13 21:52:19 by latigo
GlobalAlloc, LocalAlloc, HeapAlloc, VirtualAlloc (etc) are for when you
need to dynamically allocate a chunk of memory. This could be a
block of varying size, multiple elements of a fixed size, linked lists,
and whatever you can think of.

Now, if you deal with a limited amount of structures with a limited
size, there probably isn't any reason to use dynamic allocation.

However, dynamic allocation can also be useful when you need
some moderate or large sized data block only sometimes in your
app, for instance a data buffer for file copying. Assuming your app
is not doing file copying all the time, always having a large buffer
allocated is a waste of memory, so it would make sense to use dynamic
allocation (and deallocation :grin: ) for that one.

Which alloc function should you choose, then? Global and LocalAlloc
should do the same under win32 (quoting MSDN: "Win32 memory management does not provide a separate local heap and global heap. ").
HeapAlloc might or might not be faster, but requires a little more code
(HeapCreate or GetProcessHeap before you can use HeapAlloc).

VirtualAlloc is pretty lowlevel, afaik the lowest memory allocation
level you can get from ring3. It allocates memory in 4k chunks (the
x86 PAGE SIZE), and you can specify protection for the pages as
well - a pretty handy api for allocating large chunks of memory, or
doing "spiffy stuff" with the memory.
Posted on 2001-10-13 21:54:33 by f0dder
latigo, config file with strings from 0-255 bytes? Simple, have a stack
(local) buffer of max string length, then go through the config file
line by line and process it :).
Posted on 2001-10-13 21:56:33 by f0dder
Quit the 'Latigo's posts optimization', will ya? :)
Thanks Agner..

Posted on 2001-10-13 23:14:54 by latigo