Hello to you all,
I'm totally new to the community, & delighted to join in :)

About memory allocation, any ideas about which is best?..
GlobalAlloc, or other Win32 memory allocation functions, or the
MASM32.lib function Alloc()?

I encountered a weird problem when I used GlobalAlloc (I'll tell you about it another day), which was
fixed when I used Alloc().

But when I saw the Heap using OllyDebugger, the size allocated
with Alloc() was totally different from the size that I specified
in the call (invoke Alloc,MemSize)!

Waiting for your thoughts & ideas..

John
Posted on 2003-08-28 12:22:10 by John Kiro
Well, win32 offers three sets of API functions to work with memory.
GlobalAlloc/GlobalLock - are for general use, I use this most of the time
VirtualAlloc etc.. - are for more deeper advanced usage, for example you will be able to change some flags on specified memory pages and things like that
HeapAlloc etc.. - working with heaps, similiar like GlobalAlloc.. the only time I used this set of procedures was when I needed resizable array in memory, which can be worked out by using HeapRealloc()

I never used MASM32 lib procs for memory allocation.
It all depends what you need, for regular allocation I recommend usage of GlobalAlloc() GlobalLock() GlobalUnlock() and GlobalFree() . Check MSDN for details.
Posted on 2003-08-28 19:00:58 by Mikky
I generally use GlobalAlloc as I usually need to allocate memory for small arrays (<4MB) for large blocks of data or arrays exceeding 4MB the VirtualAlloc method is apparently more efficient.
Posted on 2003-08-28 19:09:20 by donkey
Regarding the GlobalAlloc and LocalAlloc functions, MSDN says
These functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions.


Anyone know of any tested time differences between GlobAlloc vs HeapAlloc?
Posted on 2003-08-28 19:54:03 by Mecurius
:)

f0dder has an article on them. http://f0dder.schwump.net/articles.htm
For me I would prefer Heap functions.
Posted on 2003-08-29 04:04:29 by roticv
I use the HeapAlloc - it's the best.
Code in most of my apps:


.data
HEAP1 dd 0
.code
malloc MACRO HowMany
invoke HeapAlloc,HEAP1,HEAP_ZERO_MEMORY,HowMany
ENDM
free MACRO What
invoke HeapFree,HEAP1,0,What
ENDM

start:
invoke GetProcessHeap
mov HEAP1,eax
malloc 4096
; now eax - ptr to those 4096 bytes

Posted on 2003-08-29 08:51:26 by Ultrano
Heap roxxx.



;INITGROWABLHEAP
INITGROWABLEHEAP MACRO hHEAP,pHEAPMEM
invoke HeapCreate,HEAP_NO_SERIALIZE or HEAP_GENERATE_EXCEPTIONS,0,
mov hHEAP,eax
invoke HeapAlloc,hHEAP,HEAP_NO_SERIALIZE or HEAP_ZERO_MEMORY or HEAP_GENERATE_EXCEPTIONS,0
mov pHEAPMEM,eax
ENDM
;----------------------------------------------
;DEINITGROWABLEHEAP
DEINITGROWABLEHEAP MACRO hHEAP,pHEAPMEM
invoke HeapFree,hHEAP,HEAP_NO_SERIALIZE,pHEAPMEM
invoke HeapDestroy,hHEAP
ENDM
Posted on 2003-08-29 10:21:59 by AceEmbler
Hi,
thank you all for all your useful notes.

Now, I said before that I had a problem with GlobalAlloc,
which is fixed with Alloc( the masm32.lib one).

Here is what happens:
The program returns (exit code =-1), from inside
GetOpenFileName (this is of course when I choose Open
from the menu).

I found the cause & think it's good idea to show you what I did wrong:
I allocated a block(of size MEM for example), & accessed
outside it (say , eax=ptr to block),
-> what I get isn't the familiar Access Violation
(or Illegal operation message), instead,
another allocated block disappears from the heap (viewed using OllyDebugger),
& the rest occurs as I described above.

Conclusion:
----------
be carefull not to miss-access a block allocated on the heap,
or else you might get a misterious bug!
(I think this bug has gone using Alloc, because Alloc allocates
a size much larger than I specify anyway).
But I think most of you agree it isn't a good idea to use this function anyway!
Posted on 2003-08-31 14:11:49 by John Kiro