'SizeOf' array in PureBasic?
'SizeOf' array in PureBasic?
Is there a way to return the size of an array in bytes in PureBasic similar to how SizeOf works in C?
			
			
									
									
						Code: Select all
Macro SizeOfArray(_Array)
  PeekI(@_Array-SizeOf(Quad))
EndMacro
Dim MyArray(10,10)
Debug SizeOfArray(MyArray())In PB x64 the output is:
			
			
									
									
						Code: Select all
2338038273043070997Code: 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.

						Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

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.
									
			
									
						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*4Ceterum censeo Carthaginem esse delendam!
						- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
- Fluid Byte
 - Addict

 - Posts: 2336
 - Joined: Fri Jul 21, 2006 4:41 am
 - Location: Berlin, Germany
 
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  
			
			
									
									Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
						Fluid Byte is correct. I'm interested in the size of the array in bytes.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
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.Mistrel wrote:Fluid Byte is correct. I'm interested in the size of the array in bytes.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
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
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
> 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.
			
			
									
									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.
						"PureBasic won't be object oriented, period" - Fred.
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... )
						( The path to enlightenment and the PureBasic Survival Guide right here... )

