Author Topic: [RESOLVED] Help with Array Pointers Needed!  (Read 4098 times)

0 Members and 1 Guest are viewing this topic.

Mike Lobanovsky

  • Guest
[RESOLVED] Help with Array Pointers Needed!
« on: August 11, 2014, 04:13:25 AM »
Hi Charles,

I have two numeric arrays labeled a[] and b[]. At some point in my code, I need to dereference the a and b labels and swap them so that a would henceforth point to the former array b[] while b would point to the former array a[]. I want to be able to reuse this functionality multiple times as my program runs.

What is the most efficient way to implement this SWAP() functionality on OxygenBasic array references?

Thanks!
« Last Edit: August 11, 2014, 05:02:57 PM by Mike Lobanovsky »

Aurel

  • Guest
Re: Help with Array Pointers Needed!
« Reply #1 on: August 11, 2014, 05:49:33 AM »
ok
the most simple  aproach should be ...
create one stackArray or tempArray and circulate

Code: [Select]
dim a[5]=>(1,2,3,4,5) as sys
dim b[5]=>(6,7,8,9,10) as sys
dim t[5]=>(0,0,0,0,0) as sys

string cr= chr(13)
string bf= ""
sys n


sub swap()
'copy b->t[]
for n=1 to 5
  t[n]=b[n]
next n
'copy a->b[]
for n=1 to 5
  b[n]=a[n]
next n
'copy t->a[]
for n=1 to 5
  a[n]=t[n]
next n
end sub

swap()
'show
for n = 1 to 5
bf = bf + str(a[n]) + cr
next n
for n = 1 to 5
bf = bf + str(b[n]) + cr
next n

print bf

.
« Last Edit: August 11, 2014, 07:43:21 AM by Aurel »

Charles Pegge

  • Guest
Re: Help with Array Pointers Needed!
« Reply #2 on: August 11, 2014, 08:29:05 AM »
Hi Mike,

You can use dynamic or static arrays, then access them with overlays whose base addresses are easily swapped:

Code: [Select]

'static arrays
sys a[0x10000], b[0x10000]

'alternative dynamic arrays
sys a at getmemory 0x10000*sizeof sys 'dynamic array
sys b at getmemory 0x10000*sizeof sys 'dynamic array

'pointers for overlay use
sys *pa, *pb

@pa=@a : @pb=@b 'initial
@pa=@b : @pb=@a 'swap

pa[n]= ...

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #3 on: August 11, 2014, 08:38:05 AM »
Thanks a lot, Charles,

This is exactly what I need.

One thing though. Is it essential that a[] and b[] be declared as sys for the sys* pa, pb overlays to work?

I don't want a[] and b[] to be sys value arrays. Actually, they should be either long ints or doubles.

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #4 on: August 11, 2014, 08:43:56 AM »
Aurel,

I asked you to delete your rave before you get a correct answer. Now see with your own eyes how pitiful you're looking with your array copy stuff.

Shame on you.

Charles Pegge

  • Guest
Re: Help with Array Pointers Needed!
« Reply #5 on: August 11, 2014, 08:50:44 AM »
Mike, this technique will work with all types. It is the (internal) basis for handling parameters passed to a procedure by reference.

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #6 on: August 11, 2014, 08:57:07 AM »
Should I understand your answer such that sys in sys *pa, *pb isn't correlated with the actual data type of the two arrays and serves only as syntactic sugar? In other words, are these two pointers actually void?

Or should I still use an appropriate data type for declaring these two pointers e.g. double *pa, *pb like in the C language?

Charles Pegge

  • Guest
Re: Help with Array Pointers Needed!
« Reply #7 on: August 11, 2014, 09:10:00 AM »
I advise using the matching types you need, for clarity and correct sizing:

long a[..]
long *pa

double a[..]
double *pa


But, if you want to bend this scheme for any reason, Oxygen will let you :)

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #8 on: August 11, 2014, 09:30:58 AM »
Oh, those Oxford graduates! :D

So, henceforward I'm all set to consider all occurences of * in Oxygen as void pointers where type specification is just syntactic sugar that will not have any effect on the outcome and/or integrity of further methematical operations performed on the contents of the arrays these overlays are pointing to.

Am I correct in my assumption? Can I be sure of that just to make my life easier, and my sleep, tighter?

Charles Pegge

  • Guest
Re: Help with Array Pointers Needed!
« Reply #9 on: August 11, 2014, 10:04:07 AM »
The term 'overlay' rather than 'pointer' facilitates a better understanding. An overlay can be mapped onto any piece of memory, by specifying its base address.

Once an overlay is set up, it is treated syntactically like any direct variable / array.

Generally speaking, It should not be necessary to use explicit '*' in expressions. If you do so then you will have to use casting for any type other than sys. sys is always the default type.

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #10 on: August 11, 2014, 10:32:33 AM »
So, let's put it this way (the following is an untested speculation written in O2):

Code: [Select]
double a[2]
sys *pa

a[1] = 1.5
a[2] = 1.5

@pa = @a

print str(pa[1] * pa[2]) cr ' assuming console output

I do not understand the physical meaning of "sys is always the default type". What type exactly? A platform-bitness dependent int pointer type or a platform-bitness dependent void pointer type (in C terms), if we're defining a sys* rather than sys? In still other words, does the sys in sys* define the bitness of * or its data type?

So is this code going to print 2.25 or 4 or abracadabra because it, respectively, either:

1. keeps track all along of a being actually an array of doubles -- all the way since the @pa = @a assignment; or

2. casts and rounds all further operations to sys (actually bitness-dependent int) type -- all the way since the @pa = @a assignment; or

3. just dereferences blindly the floating-point bits of array contents' doubles to sys (bitness-dependent int) bits -- discarding the array's initial data type and confusing its bit length at the point of assignment?
« Last Edit: August 11, 2014, 10:45:02 AM by Mike Lobanovsky »

Charles Pegge

  • Guest
Re: Help with Array Pointers Needed!
« Reply #11 on: August 11, 2014, 10:54:35 AM »
3: abracadabra

The only connection between a and pa is an address assignment:

@pa=@a

Mike Lobanovsky

  • Guest
Re: Help with Array Pointers Needed!
« Reply #12 on: August 11, 2014, 11:11:21 AM »
Understood.

Thank you. You were most helpful in this investigation.

[UPD] And confirmed practically.

Code: [Select]
double a[2]
sys *pa

a[1] = 1.5
a[2] = 1.5

@pa = @a

print str((double)pa[1] * (double)pa[3]) ' spawns a 2.25 message box

This discovery doesn't however eliminate the necessity to keep track of initial data types and indirectly, their lengths. So OxyLISP is going fully double precision as of now.
« Last Edit: August 11, 2014, 11:58:53 AM by Mike Lobanovsky »