Array Bounds

Just starting out? Need help? Post your questions and find answers here.
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Array Bounds

Post 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
Missed it by that much!!
HK
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The following outputs the total number of entries.

Code: Select all

Dim a(12)
Debug PeekL(a()-8)  ;Outputs 13 elements.
I may look like a mule, but I'm not a complete ass.
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Post by harkon »

Beautiful, thanks a whole bunch. Way too easy!!
Missed it by that much!!
HK
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post by PB&J Lover »

But why does this work?
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post 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
~Dreglor
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post 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
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Post Reply