Qbasicnews.com

Full Version: Copying Contents of an Array without Loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Is there a way to do this?
If you just need to use the same data without modifying it, then you could use a pointer.

Anonymous

Code:
#include "crt.bi"

dim r( 3 ) as integer
dim p( 3 ) as integer


r( 0 ) = 7
r( 1 ) = 3
r( 2 ) = 3
r( 3 ) = 1

memcpy( @p( 0 ), @r( 0 ), len( integer) * 4 )

for ui = 3 to 0 step - 1

  ? p( ui )

next
sleep
To clarify, Torahteen,

Quote:
Code:
memcpy( @p( 0 ), @r( 0 ), len( integer) * 4 )

memcpy is included in the C runtime library, so you have to include crt.bi and compile with the crt lib.

the last argument must be len(datatype) * number of subscripts

So if you have the array

Code:
dim blarg(19) as byte

you need to use

len(byte) * 20

(remember the 0th subscript)
bwah! i was trying to find something like that, i was thinking to myself 'isnt there a memcopy routine or something like that...' but i couldnt find it... and that explains why, heh, its c >)
Yo! Syn! Hows ZGA coming :)

The newest pics of the ships are... stunning.
Quote:...
So if you have the array
Code:
dim blarg(19) as byte
you need to use len(byte) * 20

(remember the 0th subscript)

Code:
dim array(20) as byte = index 0 to 19 = 20 bytes
'but
dim array(19) as byte = index 0 to 18 = 19 bytes
'or
dim dim array(1 to 20) as byte = index 1 to 20 = 20 bytes

Or i'm wrong ?

Joshy
Or you can use the fast MMX copy sub from the gfx library.

Joshy
Code:
#include "fbgfx.bi"
declare sub mmxcopy alias "fb_hMemCpyMMX" (byval dest as any ptr, byval src as any ptr, byval size as integer)

const MB =1024*1024

dim source(MB) as byte
dim destination(MB) as byte
dim i as integer
for i=0 to MB-1
  source(i)=rnd*255
next

print "begin copy 250 MB"
for i=1 to 250
  mmxcopy @destination(0),@source(0),MB
next
print "ready"
sleep
Quote:
Code:
dim array(20) as byte = index 0 to 19 = 20 bytes

If you dimension an array using (20), it will dimension index 0 to 20, giving 21 subscripts.
Exactly. In C is the way you say. That's 'cause of this:

In C:

Code:
int a[number-of-subscripts];

In BASIC:

Code:
Dim a (highest-subscript) As Integer

The semantics are different.
Pages: 1 2