Copy array to a memory block and vice verse

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Copy array to a memory block and vice verse

Post by blueznl »

I have to juggle a bunch of arrays, and they are all processed in some other (older) code. Each array is 2 dimensional, but can have different sizes.

To speed things up, I'd rather do a binary copy of the whole array (containing only Int data) instead of the typical 2 dimensional nested for / next loops.

So, is there something to help me speed up things, and replace this:

Code: Select all

dim a(x_size,y_size)
for x = ...
  for y = ...
    a(x,y) = b(x,y)
  next y
next x
process array a()
... with something like this:

Code: Select all

dim a(x_size,y_size)
copy array b() over array a()
process array a()
Are arrays continuous blocks of allocated memory? I guess, if so, I could do a memory copy?
( 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... )
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Copy array to a memory block and vice verse

Post by STARGÅTE »

blueznl wrote: Sat Feb 04, 2023 7:58 pm Are arrays continuous blocks of allocated memory? I guess, if so, I could do a memory copy?
Yes, that's true.
You can do:

Code: Select all

CopyMemory(@b(), @a(), (ArraySize(b(),1)+1)*(ArraySize(b(),2)+1)*SizeOf(Integer))
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Copy array to a memory block and vice verse

Post by mk-soft »

But check if the target array is big enough ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Copy array to a memory block and vice verse

Post by Demivec »

Use:

Code: Select all

CopyArray(b(), a())
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Copy array to a memory block and vice verse

Post by blueznl »

Demivec wrote: Sat Feb 04, 2023 8:49 pm Use:

Code: Select all

CopyArray(b(), a())
Yeah, that's what I normally would use, but I might have a random number of arrays, but I don't know how many upfront, and I don't want to create 'n' arrays upfront, and then I have to keep track of which array was used for what.

Darn, it's hard to explain :-)
( 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... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Copy array to a memory block and vice verse

Post by blueznl »

mk-soft wrote: Sat Feb 04, 2023 8:23 pm But check if the target array is big enough ...
Yeah, I'll redim the destination just before running the memcopy.
( 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... )
Post Reply