News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Array question

Started by ragdog, March 27, 2011, 06:29:29 PM

Previous topic - Next topic

ragdog

Hi

I have a question to an string Array.

I enumerate processes and store the strings store it to an array "processname strings lenght is unknow"
must i before i strore this strings in a Array alloctate memory for it?

I think use i not alloctate memory overwrite it other entry in the data? section if not enough space or not?

example:

hArry    db 20 dup(?)
hOtherHandle dd ?

Have this file more as 20bytes  entry overwrite it my hOtherhandle

How i can get the enough size for Allocate the memory?
And can i make this with a struct?

Regards,

Twister

You can allocate more memory using GlobalAlloc. It will give you a handle of the new block of memory, so then you pass that to GlobalLock to get the address (void*) of your newly allocated memory for free use.  :U

qWord

here an quick & dirty exsample for an dynamic growing string array (pointer array):
Quoteinclude masm32rt.inc
.code

init_str_tbl proc

    mov eax,alloc(4)
    ret
   
init_str_tbl endp

add_str proc uses esi ebx ppsz: ptr PCHAR,psz: ptr CHAR
   
    mov eax,ppsz
    xor esi,esi
    .while DWORD ptr [eax+esi]
        lea esi,[esi+4]
    .endw
    mov ebx,rv(GlobalReAlloc,ppsz,ADDR [esi+8],GMEM_ZEROINIT or GMEM_MOVEABLE)
    lea eax,[len(psz)+1]
    mov DWORD ptr [ebx+esi],alloc(eax)
    invoke szCopy,psz,eax
    mov eax,ebx
    ret
   
add_str endp

main proc
LOCAL ppsz: ptr PCHAR   
   
    mov ppsz,rv(init_str_tbl)
    mov ppsz,rv(add_str,ppsz,input("string1: "))
    mov ppsz,rv(add_str,ppsz,input("string2: "))
    mov ppsz,rv(add_str,ppsz,input("string3: "))

    ; ppsz is an pointer to an pointer array

    mov esi,ppsz
    xor edi,edi
    .while DWORD ptr [esi+edi*4]
        print "string Nr. "
        print str$(ADDR [edi+1])
        print ": "
        print PCHAR ptr [esi+edi*4],13,10

        ; free memory/string
        ; free PCHAR ptr [esi+edi*4]

        lea edi,[edi+1]
    .endw
   
    ; free ppsz
   
   
    inkey
    exit
   
main endp
end main
FPU in a trice: SmplMath
It's that simple!

Tedd

There are numerous options, depending how complicated you want to make it...

  • allocate a large block of memory, and hope it's enough (max_process_name_length * max_num_processes)
  • enumerate the process list in two passes; the first just sums the string lengths without saving them, then allocate the required memory, and the second pass can store the strings
  • create an array of pointers, and allocate a new block of memory for each string, storing the pointer in the array
  • allocate a large block, fill it up, and expand (realloc) if needed
  • ...many others, mainly involving dynamic allocation...
No snowflake in an avalanche feels responsible.

dedndave

it is likely that a large allocation will work - there are only so many processes running
but - the 2 pass method seems prettier   :P

ragdog

Thanks for your help
This array tips have i saveit i need it later  :U
Why? I use now EnumProcess this have allready a pointer to an array  :bg

Greets,

donkey

If speed is less critical than reliability, ease of use and scalability (which is almost always the case), you can use the DSA functions from common controls

MAX_PROCESS_NAME equ 64 ; arbitrary value

PROCESS_NAME struct
    PID DD ?
    szName DB MAX_PROCESS_NAME DUP (?)
PROCESS_NAME ends

; Allocate a working structure
pronam PROCESS_NAME <>

; Create the array with 64 initial items
; it will grow by this number as needed
invoke DSA_Create, SIZEOF PROCESS_NAME, 64
mov hDSA, eax

; fill the working array with the data
mov eax, pid
mov pronam.PID, eax
invoke lstrcpyn, offset pronam.szName, offset TheProcessName, MAX_PROCESS_NAME-1

; Add an item
invoke DSA_InsertItem, hDSA, DSA_APPEND, offset pronam

; retrieve an item by item number (1). Fills pronam with the data
invoke DSA_GetItem, hDSA, 1, offset pronam

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable