Page 1 of 1
Pass array by reference and allocate inside proc?
Posted: Tue Dec 04, 2007 3:57 am
by PurePWNRER
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?
Posted: Tue Dec 04, 2007 5:32 am
by Mistrel
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]
Posted: Tue Dec 04, 2007 6:22 am
by PurePWNRER
Did I forget to mention they are multidimensional and dynamic?, 3 dimensions to be exact.
I wouldn't be asking if that wasn't the case

Posted: Tue Dec 04, 2007 7:29 am
by PurePWNRER
Perhaps I'll have to allocate the memory and calculate the displacement / offsets by my own?, how could I calculate this? (3 dimensions, byte array).
Posted: Tue Dec 04, 2007 7:36 am
by citystate
offset = z+(y+x*width)*height?
...or the like
Posted: Tue Dec 04, 2007 7:37 am
by Mistrel
I think you're making things a lot harder for yourself than they need to be. If you could be more specific about what you're trying to accomplish we may be able to help you simplify your problem.
Posted: Tue Dec 04, 2007 8:11 am
by PurePWNRER
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.
Posted: Tue Dec 04, 2007 8:53 am
by Dare
Having ESP we had all, of course, understood perfectly what you wanted to do even before you gave us some sort of explanation in your last post.

Posted: Tue Dec 04, 2007 9:18 am
by Mistrel
My ESP must be broken.
Posted: Tue Dec 04, 2007 12:01 pm
by Trond
If you want speed, use AllocateMemory() and pointers.
Posted: Tue Dec 04, 2007 12:38 pm
by Heathen
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))
Posted: Tue Dec 04, 2007 1:13 pm
by blueznl
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...
Posted: Tue Dec 04, 2007 2:59 pm
by Trond
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.
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.