News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

How to get size of an array using a pointer to the array

Started by jmw457, July 10, 2017, 02:23:22 AM

Previous topic - Next topic

jmw457

I have created a DLL with the following header:

FirstEntry PROC AllFour:PTR DWord

where the pointer AllFour points to a DWord array. 

Now I want to get size of the array pointed to by AllFour in bytes.  I have tried several things (mov eax,AllFour and mov eax,OFFSET AllFour), but I can't get the size of the buffer. 

Thanks for any help with finding the size of the array pointed to by AllFour. 


HSE

Equations in Assembly: SmplMath

jj2007

Hi,

Welcome to the forum :icon14:

As HSE already asked: Is it declared somewhere? If yes, pass lengthof:include \masm32\include\masm32rt.inc

.data
MyArray dd 25, 18, 23, 17, 9, 2, 6

.code
FirstEntry PROC ArrLen:DWORD, AllFour:PTR DWord
  inkey str$(ArrLen), " elements", 13, 10
  ret
FirstEntry endp
start:
  invoke FirstEntry, lengthof MyArray, offset MyArray
  exit

end start


If you don't know the size, but you are sure that there is a delimiter, e.g. zero, make a loop and check it...

Sometimes libraries provide macros to get the size of the array, e.g. MyArray(?):

include \masm32\MasmBasic\MasmBasic.inc      ; download
.code
  Init
  Dim MyArray() As DWORD
  For_ ecx=0 To Rand(1000)      ; random # elements
      mov MyArray(ecx), Rand(12345)      ; random elements
  Next
  push VarPtr(MyArray(0))
  call FirstEntry
  Exit

FirstEntry PROC AllFour:PTR DWord
  Inkey Str$("The array has %i elements", MyArray(?))
  ret
FirstEntry endp

EndOfCode

jmw457

Thanks HSE and jj2007.  I solved it by passing the length of the array as the first element.  But jj's solution is more elegant so I'll try that.  If there is any problem, I will post back.  Much appreciated.