Page 1 of 1

Array Bounds

Posted: Mon Dec 04, 2006 6:08 pm
by harkon
Does anyone know an easy way to find the array bounds of an existing array? It would be like the Ubound and Lbound functions in VB.

tia

Posted: Mon Dec 04, 2006 6:11 pm
by srod
The following outputs the total number of entries.

Code: Select all

Dim a(12)
Debug PeekL(a()-8)  ;Outputs 13 elements.

Posted: Mon Dec 04, 2006 6:28 pm
by harkon
Beautiful, thanks a whole bunch. Way too easy!!

Posted: Wed Dec 06, 2006 4:59 am
by PB&J Lover
But why does this work?

Posted: Wed Dec 06, 2006 5:46 am
by Dreglor
theres a simple structure before the array like in Linked lists

my guess it something like this

Code: Select all

Structure PB_Array
  NumberOfElements.l
  SizeOfElement.l
EndStructure

Posted: Wed Dec 06, 2006 2:41 pm
by PB&J Lover
That doesn't explain why PeekL(a()-8) returns the bounds of the array. Why does it work technically?

What does the code do?

DB

Posted: Wed Dec 06, 2006 2:49 pm
by srod
a() simply returns the address of the array in memory (that is the array contents). I could have used @a() which I guess would make it more obvious.

Purebasic places some information pertinent to the array before the array itself. For example, at the location 8 bytes before the array contents (as referenced by a()-8 ), Purebasic makes a note of how many elements there are in the array.

The statement

Code: Select all

PeekL(a()-8)
thus returns the number of elements in the array.