Page 1 of 2

SizeOf( Array() ) ?

Posted: Sun Nov 09, 2003 7:44 pm
by Codemonger
Does anybody know how to get the sizeof() an array.... not the upper or lower bounds of the elements but the actually length in bytes of the whole array ... or at least the length in bytes of one array ?

Posted: Sun Nov 09, 2003 8:45 pm
by freak

Code: Select all

Procedure.l ArrayMemorySize(*Array)
  !sub [esp], dword 8
  !push dword [esp]
  !push dword 0
  !push dword [PB_MemoryBase]
  !call _HeapSize@12
  ProcedureReturn
  HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined
EndProcedure

Procedure ArraySize(*Array)
  ProcedureReturn PeekL(*Array-8)
EndProcedure


Dim Array.l(50)


Debug ArrayMemorySize(@Array())
Debug ArraySize(@Array())
ArrayMemorySize() shows the real size in memory.
ArraySize() shows the total number of elements.

Timo

Posted: Sun Nov 09, 2003 8:46 pm
by Codemonger
Thanks Freak your a lifesaver ...

Posted: Sun Nov 09, 2003 10:52 pm
by GPI
How secure is this for future versions of PB.

Posted: Sun Nov 09, 2003 11:01 pm
by freak
> How secure is this for future versions of PB.

Do you know of much other ways to allocate an Array than HeapAloc ??
If something changes, what's the deal of changing this procedure, too ?

Timo

Posted: Sun Nov 09, 2003 11:22 pm
by fsw
freak wrote: Procedure.l ArrayMemorySize(*Array)
!sub [esp], dword 8
!push dword [esp]
!push dword 0
!push dword [PB_MemoryBase]
!call _HeapSize@12
ProcedureReturn
HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined
EndProcedure
Freak I have a question for you:

HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined

comes after:

ProcedureReturn

and I thought ProcedureReturn is like a break because it exits the Procedure right away.
So:

HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined

will never be executed... or am I wrong :?:

Posted: Sun Nov 09, 2003 11:26 pm
by freak
Freak I have a question for you:

HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined

comes after:

ProcedureReturn

and I thought ProcedureReturn is like a break because it exits the Procedure right away.
So:

HeapSize_(0, 0, 0) ; make sure, _HeapSize@12 is defined

will never be executed... or am I wrong
Yes, you are right, and calling HeapSize with a lot of 0's would definately
fail.

I call the HeapSize directly from asm, ans since it is external, it has to
be defined with

!extrn _HeapSize@12

in order to be used. The problem is, as soon as somebody uses
HeapSize_(), PB will automatically define the symbol, and you get an
'allready defined' error. So, to prevent that, i just insert HeapSize_() at
some point, where it will never be executed, just so that PB defines the
symbol for me.

Dirty little trick, i know, but... :wink:

Timo

Posted: Mon Nov 10, 2003 9:29 am
by Rings
very handy for the a new UBOUND(Array) Command:

Code: Select all

Dim MYArray.b(4711)

Procedure Ubound(*Array)
 ProcedureReturn PeekL(*Array-8)
endprocedure

Debug Str(UBound(@MyArray(0)))

Posted: Thu Dec 23, 2004 11:47 am
by Falko
Hi @freak

And have you a code to make 'Lbound'?

Thanks Falko

Posted: Thu Dec 23, 2004 3:32 pm
by Rings
Procedure LBound(*Array)
procedurereturn 0
endprocedure

Posted: Thu Dec 23, 2004 3:42 pm
by helpy
Rings wrote:Procedure LBound(*Array)
procedurereturn 0
endprocedure
:P :lol:

Posted: Thu Dec 23, 2004 4:13 pm
by Falko
thanks @Rings, but this Source have many errors. It does not rearly help.
Here the german link.

http://robsite.de/php/pureboard/viewtopic.php?t=1318

Can any help me please to corecting this source?

Sorry, my english from german post,

Falko

Posted: Thu Dec 23, 2004 4:18 pm
by helpy
Falko wrote:thanks @Rings, but this Source have many errors. It does not rearly help.
Here the german link.

http://robsite.de/php/pureboard/viewtopic.php?t=1318

Can any help me please to corecting this source?

Sorry, my english from german post,

Falko
------GERAMAN---------------
Hi Falko,

LBound in PureBasic ist IMMER 0 !!!

In anderen Sprachen ist es möglich ein Array zu definieren, das von 50 bis 100 geht, wobei LBOUND in diesem Fall 50 ist. Würde man einen Index außerhalb dem Bereich [50-100] aufrufen würde es eine Fehlermeldung geben.

------ENGLISH---------------
LBound in PureBasic is ALWAYS 0 !!!

In otzer programming languages it is possible to define arrays (i.e.) from 50 to 100. In this case LBOUND would return 50. If you would try to get an index outside the range [50-100] an error would occur.

cu, helpy

Posted: Thu Dec 23, 2004 4:51 pm
by Falko
Thanks @helpy
Than i can't convert this Source from Powerbasic to Purebasic.
:roll:

It's very usefull to make a own Excel-Sheet over Purebasic. :)

Falko

Posted: Thu Dec 23, 2004 5:08 pm
by helpy
Falko wrote:Thanks @helpy
Than i can't convert this Source from Powerbasic to Purebasic.
:roll:
Why not? As I can see, in the original code there are only zero based arrays used. So ... instead of LBound(...) you can insert the value "0" (zero).

cu, helpy