Author Topic: How to get size of an array using a pointer to the array  (Read 182 times)

jmw457

  • Regular Member
  • *
  • Posts: 8
How to get size of an array using a pointer to the array
« on: July 10, 2017, 02:23:22 AM »
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

  • Member
  • ****
  • Posts: 532
  • <AMD>< 7-32>
Re: How to get size of an array using a pointer to the array
« Reply #1 on: July 10, 2017, 04:19:15 AM »
Welcome!!

How you declare the size of array?

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: How to get size of an array using a pointer to the array
« Reply #2 on: July 10, 2017, 06:03:04 AM »
Hi,

Welcome to the forum :icon14:

As HSE already asked: Is it declared somewhere? If yes, pass lengthof:
Code: [Select]
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

  • Regular Member
  • *
  • Posts: 8
Re: How to get size of an array using a pointer to the array
« Reply #3 on: July 10, 2017, 06:52:43 AM »
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.