'SizeOf' array in PureBasic?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

'SizeOf' array in PureBasic?

Post by Mistrel »

Is there a way to return the size of an array in bytes in PureBasic similar to how SizeOf works in C?
cxAlex
User
User
Posts: 88
Joined: Fri Oct 24, 2008 11:29 pm
Location: Austria
Contact:

Post by cxAlex »

Code: Select all

Macro SizeOfArray(_Array)
  PeekI(@_Array-SizeOf(Quad))
EndMacro

Dim MyArray(10,10)

Debug SizeOfArray(MyArray())
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

In PB x64 the output is:

Code: Select all

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

Post by srod »

ArraySize() will give you the upper bound for an individual dimension if this helps? My apologies if you are already aware of this function.
I may look like a mule, but I'm not a complete ass.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Macro SizeOfArray(_Array)
  PeekI(@_Array-SizeOf(Quad) * 2)
EndMacro
CompilerElse
Macro SizeOfArray(_Array)
  PeekI(@_Array-SizeOf(Quad))
EndMacro
CompilerEndIf

Dim MyArray(10,10)

Debug SizeOfArray(MyArray())
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Here's my attempt:

Code: Select all

Macro SizeOfArray(_Array, type, dimension = 1)
  (ArraySize(_Array,dimension) + 1) * SizeOf(type)
EndMacro

Dim MyArray(10,10)

Debug SizeOfArray(MyArray(), Integer, 1) * SizeOfArray(MyArray(), Integer, 2)


Dim Another.BITMAP(15)

Debug SizeOfArray(Another(), BITMAP)
Last edited by Demivec on Fri Jun 05, 2009 10:40 am, edited 1 time in total.
thearr
User
User
Posts: 21
Joined: Sun Apr 26, 2009 3:18 am
Location: RU

Post by thearr »

My way:

Code: Select all

Procedure SizeOfArray(*Array)
  ProcedureReturn PeekL(*Array-8)*PeekL(*Array-20)
EndProcedure

Dim MyArray.l(10,10)
Debug SizeOfArray(MyArray())
Debug 11*11*4
Ceterum censeo Carthaginem esse delendam!
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Funny that you guys still prefer peeking around undocumented memory locations even after we added the ArraySize() command. :roll:
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes, a very unexpected topic to be sure :?
BERESHEIT
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Am I missing something? I thought they wanted to obtain the size in bytes. That is something that can't be done with ArraySize() since it only returns the number of elements. Sure you could multiply with that value but there is no ArrayType() command. Not yet :wink:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Fluid Byte wrote:Am I missing something? I thought they wanted to obtain the size in bytes. That is something that can't be done with ArraySize() since it only returns the number of elements. Sure you could multiply with that value but there is no ArrayType() command. Not yet :wink:
Fluid Byte is correct. I'm interested in the size of the array in bytes.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Mistrel wrote:
Fluid Byte wrote:Am I missing something? I thought they wanted to obtain the size in bytes. That is something that can't be done with ArraySize() since it only returns the number of elements. Sure you could multiply with that value but there is no ArrayType() command. Not yet :wink:
Fluid Byte is correct. I'm interested in the size of the array in bytes.
Actually the ArraySize() command is misleading because, as was pointed out, it's not the size of the Array you are getting, but the number of elements.

IMHO it would have been better to name this command ArrayElements() (or something similar). This way ArraySize() would have been free to use as command to get the real size in bytes.

BTW: The same goes for ListSize...

bye
fsw
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

With the current commands it's as simple as x*y now, imho adding a command such as TotalArrayBytes or such is analagous to sending your remote control out to get hydraulic-assisted buttons installed. Just my 2 cents, no offense meant to anyone.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> IMHO it would have been better to name this command ArrayElements()

This has all been debated before in another heated thread, and the result
was to keep it named as ArraySize(). No need to go over it again.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

So, all that is needed is a feature requst called: ArrayByteSize :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply