Pass array by reference and allocate inside proc?

Just starting out? Need help? Post your questions and find answers here.
PurePWNRER
User
User
Posts: 45
Joined: Mon Mar 27, 2006 5:44 am

Pass array by reference and allocate inside proc?

Post 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?
:/ My kingdom for a snippet!
I might bash, But I provide.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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]
PurePWNRER
User
User
Posts: 45
Joined: Mon Mar 27, 2006 5:44 am

Post 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 :)
:/ My kingdom for a snippet!
I might bash, But I provide.
PurePWNRER
User
User
Posts: 45
Joined: Mon Mar 27, 2006 5:44 am

Post 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).
:/ My kingdom for a snippet!
I might bash, But I provide.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

offset = z+(y+x*width)*height?

...or the like
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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.
PurePWNRER
User
User
Posts: 45
Joined: Mon Mar 27, 2006 5:44 am

Post 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.
:/ My kingdom for a snippet!
I might bash, But I provide.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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. :P :twisted:
Dare2 cut down to size
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

My ESP must be broken.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

If you want speed, use AllocateMemory() and pointers.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post 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))
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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...
( 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... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply