One thing that has puzzled me a while since I've been dabbling with 32bit asm is how one should deal with data of variable length. Sometimes providing a large buffer to hold your info isn't convenient. For the program I'm currently making I would like to create an array of structures (not too hard) but have the user define how many members there are to this array. In Visual Basic this is easy as I can create an array like so:

Dim MyArray() As MyStruct

and then when I want to define the number of items in the array:

ReDim MyArray(20)

I'm converting my program to masm32 now but I don't know how to deal with this kind of problem. The same thing applies to strings too. I wouldn't mind having the flexibility of variable strings like VB uses rather than use buffers which have the danger of overflowing or using too much memory to begin with.
Posted on 2004-07-09 20:19:26 by DeX
Here's a simple function which will grow a byte array dynamically.

TestFunc proc uses ebx esi
; esi contains the pointer to the allocated buffer for use
; ebx contains the ProcessHeapID.

; start with a 1k buffer
mov ecx, 1024

; allocate the buffer now
invoke GetProcessHeap
mov ebx, eax ; keep the ProcessHeapID for future reference

invoke HeapAlloc, eax, HEAP_ZERO_MEMORY, ecx
mov esi, eax

; do something with the 1k buffer here

; now, change the buffer size to 2k
mov ecx, 2048

; realloc the new buffer while preserving the original contents
invoke HeapReAlloc, ebx, HEAP_ZERO_MEMORY, esi, ecx

; do something with the 2k buffer here

; free buffer when done
invoke HeapFree, ebx, 0, esi

TestFunc endp

Posted on 2004-07-09 21:16:19 by Relvinian

As Relvinian said you can use Heap function because Heap are resizable memory allocation .

HeapCreate ;give handle to the heap
HeapAlloc ;give a pointer to the heap
HeapReAlloc ; resize or move a heap
HeapDestroy ; ... :rolleyes: self-explained
Posted on 2004-07-09 22:43:27 by Neitsa
Thanks, I just tried this code and it works nicley. It should let me use dynamic arrays easily now. Cheers :alright:
Posted on 2004-07-13 14:16:21 by DeX