[futurebasic] Re: [FB] Records, Pointers & Confused

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2001 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Tue, 17 Apr 2001 15:32:17 +0200

Stuart Price wrote:
> 
> Sorry that last post should have read (weird emailness):
> 
> On Tue, 17 April 2001, Robert Purves wrote:
> 
> >
> > There are two kinds of valid assignment involving entire records:
> >
> > DIM r1 as TestRec
> > DIM r2 as TestRec
> > DIM rPtr as ptr to TestRec
> > r1 = r2   ' assign:  record = record
> > r1 = rPtr ' assign:  record = recordPtr
> 
> This makes sense but trying it out has caused me some confusion.  Please try the code snippet below:
> 
> --
> 
> ' Run in console
> '
> BEGIN RECORD TestRec
> DIM s AS STR255
> END RECORD
> 
> DIM r1 as TestRec
> DIM r2 as TestRec
> DIM rPtr as ptr to TestRec
> 
> end globals
> 
> r1.s = "Hello"
> r2.s = "World"
> 
> print r1.s,r2.s ' Prints Hello World
> print
> 
> rPtr = r2
> r1 = rPtr
> 
> print r1.s,r2.s ' Prints World World
> print
> 
> r1.s = "Hello"
> r2.s = "World"
> 
> print r1.s,r2.s ' Prints Hello World
> print
> 
> I was expecting the last PRINT to display 'World World' as I have set r1 = rPtr and rPtr is pointing to r2?
> 
> TIA
> 
> Stu

Hi Stu,

I think your confusion comes from the fact that the following assignment:

 r1 = rPtr ' assign:  record = recordPtr

can be seen as a built-in shorthand in lieu of a BLOCKMOVE
statement. Basically, the correct or let's say usual syntax should
have been:

r1;_recSize = rPtr
or
BLOCKMOVE rPtr,@r1,_recSize

The shorthand allowed by FB could be somewhat misleading since we
might expect that the reverse could also be done like this:

rPtr = r1

The above doesn't work because rPtr is actually an address in memory
whereas r1 is a record and FB doesn't provide a shorthand in that
direction. You must use then a BLOCKMOVE statement:

BLOCKMOVE @r1,rPtr,_recSize

The above syntax copy the data in your record at the memory location
pointed to by your pointer.

Notice also that you can set the pointer to your record's address
like so:

rPtr = @r1

In which case you are creating a sort of alias of your record. If
you alter fields using rPtr (whose value is now the address of r1 in
memory), you actually modify the fields of r1.

-- 

Cheers

Alain

-----------------------------------------------------
FB^3 in Europe:  http://euro.futurebasic.com/
FB II Pouch:     http://www.pixmix.com/FB/outils.html
-----------------------------------------------------