Pass array by reference and allocate inside proc?
-
- User
- Posts: 45
- Joined: Mon Mar 27, 2006 5:44 am
Pass array by reference and allocate inside proc?
How can I allocate arrays in a procedure, to later on pass by reference to other procedures? I don't want to use global / shared method, I need to pass the actual pointer of the array to later on access the array inside each proc. I also need to allocate the array inside a procedure (ie it returns a lptr that points to the array).
Possible?
Possible?
:/ My kingdom for a snippet!
I might bash, But I provide.
I might bash, But I provide.
Code: Select all
Structure array
a.l[200]
EndStructure
Procedure this()
*n.array=AllocateMemory(SizeOf(array))
*n\a[1]=42
ProcedureReturn *n
EndProcedure
*n.array=this()
Debug *n\a[1]
-
- User
- Posts: 45
- Joined: Mon Mar 27, 2006 5:44 am
-
- User
- Posts: 45
- Joined: Mon Mar 27, 2006 5:44 am
-
- User
- Posts: 45
- Joined: Mon Mar 27, 2006 5:44 am
lol? I dont want easy I want speed and theres a reason of why one would interleave pixels, even though im using planar representation, the reason you would go about manipulating pixels this way is speed. when you must get pixels from certain coordinates and you dont use any extensions such as mmx or sse2 you must at least manipulate the data in this way.
if all you'll do is lame linear manipulation on pixel data then fine just move byte through byte and manipulate but the day you even think of convolving the data, then you'll wish you went for the "hard" way.
and yes im using the gpu but this is the software fallback code.
the pixel format is BGRA8, no windows api used.
if all you'll do is lame linear manipulation on pixel data then fine just move byte through byte and manipulate but the day you even think of convolving the data, then you'll wish you went for the "hard" way.
and yes im using the gpu but this is the software fallback code.
the pixel format is BGRA8, no windows api used.
:/ My kingdom for a snippet!
I might bash, But I provide.
I might bash, But I provide.
something like this?
Code: Select all
Macro arrayoff(x,y,z,width,height,Bytes)
(((width*height*z)+y*width+x)*Bytes)
EndMacro
Procedure test()
width = 100
height = 100
zheight = 100
Bytes = 4
Value = 100
*mem = AllocateMemory(width*height*zheight*Bytes)
PokeL(*mem+arrayoff(99,99,99,width,height,Bytes),Value)
ProcedureReturn *mem
EndProcedure
width = 100
height = 100
Bytes = 4
*mem = test()
Debug PeekL(*mem+arrayoff(99,99,99,width,height,Bytes))
I don't get it. Arrays are always passed on by reference when using them as procedure parameters. As such it doesn't matter much of you use them as globals or not.
I doubt that you would access the elements of an array faster by storing it into allocated memory blocks and 'picking' the elements yourself.
It may indeed be better if you could give us a glimpse of what you are trying to accomplish...
I doubt that you would access the elements of an array faster by storing it into allocated memory blocks and 'picking' the elements yourself.
It may indeed be better if you could give us a glimpse of what you are trying to accomplish...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Picking the individual elements won't be faster, but you can optimize out the picking and access the memory linearly instead of randomly. An array access means one or more multiplications. If you use a pointer, you can just add 4 to the pointer every iteration. That gives a speed increase.blueznl wrote:I doubt that you would access the elements of an array faster by storing it into allocated memory blocks and 'picking' the elements yourself.