The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Astro on September 12, 2009, 01:48:32 AM

Title: Arrays of Structures
Post by: Astro on September 12, 2009, 01:48:32 AM
 :bg

I feel like I'm starting to get somewhere now.

Sorry if I'm sounding like a broken record over this...

Given what has been said in the past regarding memory, etc.. if I wanted to increase the number of elements of t from 99 to 5000 progmatically, can it be done?

.386
.model flat,stdcall

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

TST struct
na dword ?
TST ends

TST2 struct
na dword ?
TST2 ends

.data?
t TST 99 dup (<>)
t2 TST2 50 dup (<>)

.data
a1 db "Array 1",0
a2 db "Array 2",0
a3 db "DELIMITER",0

.code

start:
int 3

mov ecx,0
mov dword ptr t.na[ecx],offset a1

mov ecx,1*sizeof TST
mov dword ptr t.na[ecx],offset a2

mov ecx,10*sizeof TST
mov dword ptr t.na[ecx],offset a1

mov ecx,5*sizeof TST
mov dword ptr t2.na[ecx],offset a1

push 0
push t.na
push t.na
push 0
call MessageBox

xor eax,eax
ret

end start


Best regards,
Astro.
Title: Re: Arrays of Structures
Post by: dedndave on September 12, 2009, 02:19:54 AM
nope
Title: Re: Arrays of Structures
Post by: hutch-- on September 12, 2009, 07:02:03 AM
Astro,

You need to dynamically allocate memory to be able to resize it. Contrary to popular opinion, the old GlobalAlloc() API wth the GMEM_FIXED flag set is a good option for doing work like this. The "alloc" macro uses it. As usual you ensure the element size is a multiple of 4 or even 8 to ensure element alignment.

The trick is to set up an array of pointers to each element, there is a procedure in the MASM32 library that does this and I have posted a faster one some months ago. It can get a bit more complicated if you want to keep resizing the array but if they are only small, just create a new one and copy the elements from the old one into it. Then delete the old array.

PS: You are doing fine, all of this stuff is complicated.  :U
Title: Re: Arrays of Structures
Post by: sinsi on September 12, 2009, 07:21:01 AM
You can use GlobalAlloc to get your original 99, then you can use GlobalRealloc to resize it and keep the contents. HeapAlloc/HeapRealloc does the same (I think).
You tend to run out after about 2GB though...
Title: Re: Arrays of Structures
Post by: daydreamer on September 12, 2009, 08:43:33 AM
Quote from: sinsi on September 12, 2009, 07:21:01 AM
You can use GlobalAlloc to get your original 99, then you can use GlobalRealloc to resize it and keep the contents. HeapAlloc/HeapRealloc does the same (I think).
You tend to run out after about 2GB though...

1:there is a XP hack, used by poser users, that allow for usage of 3gb
2:if you run a 64bit OS, your 32bit application can make full use of 4gb in emulation mode