hi
lets say i define an array 1 to 10 but i want to be able to add in thr run time to add him "places in the memory"

for example

mystruct strcut

mystruct ends

myarray mystruct 10 dup <?>


no i have an array of 10 struct and i want to be able to add in the run time more . (i cant know how much i need before the run )

any solutions?

thanks
eko
Posted on 2004-07-20 07:27:57 by eko
HeapAlloc and HeapReAlloc?
Posted on 2004-07-20 08:13:14 by roticv
How I would do it:



.data
mystruct struct
value1 dd 0
..
mystruct ends

.data?
mystructptr dd ?


.code
;to allocate it, some number x multiplied by size of mystruct
; you can just say x*mystruct but it can get confusing if you use it throughout your code

invoke GetProcessHeap
invoke HeapAlloc,eax,HEAP_ZERO_MEMORY, x*sizeof mystruct ; x* (size of mystruct pointer)
mov [mystructptr],eax ;the actual structure 'object' as I call it

;to clean it up
invoke GetProcessHeap
invoke HeapFree, eax, NULL, mystructptr

;Not sure how to realloc it but it should be easy enough

;to use:
mov ecx, mystructptr ;point ecx to block of memory at mystructptr
mov edx, index ;which array to point to
imul edx, sizeof mystruct ;don't forgit to scale it doh!

mov eax,[ecx+edx].mystruct.value1
Posted on 2004-07-20 09:17:14 by drarem

How I would do it:



.data
mystruct struct
value1 dd 0
..
mystruct ends

.data?
mystructptr dd ?


.code
;to allocate it, some number x multiplied by size of mystruct
; you can just say x*mystruct but it can get confusing if you use it throughout your code

invoke GetProcessHeap
invoke HeapAlloc,eax,HEAP_ZERO_MEMORY, x*sizeof mystruct ; x* (size of mystruct pointer)
mov [mystructptr],eax ;the actual structure 'object' as I call it

;to clean it up
invoke GetProcessHeap
invoke HeapFree, eax, NULL, mystructptr

;Not sure how to realloc it but it should be easy enough


invoke GetProcessHeap
invoke HeapReAlloc, HEAP_ZERO_MEMORY, , <newsize>



;to use:
mov ecx, mystructptr ;point ecx to block of memory at mystructptr
mov edx, index ;which array to point to
imul edx, sizeof mystruct ;don't forgit to scale it doh!

mov eax,[ecx+edx].mystruct.value1


Relvinian
Posted on 2004-07-20 12:30:13 by Relvinian
thanks alot!
Posted on 2004-07-21 10:04:56 by eko
For anyone else stuck with the problem of TRUE dynamic arrays (ones that grow on demand) you may wish to look at the codebase for the CVector and CArrayManager classes I've posted on this board...

CVector is a class which manages arrays of dwords, it has methods with names like "push_back" and "pop_back".

CArrayManager is similar, but handles arrays of elements whose size is determined at runtime - you tell the array how large its entries are, you can use its modified push and pop methods to store and retrieve whole structs.

Since these are OOP classes, you can create any number of arrays, as each array is an "instance of the class object". That means the same CODE is used for any number of arrays, but each array contains unique data.
Posted on 2004-07-21 12:09:24 by Homer