[comp.lang.pascal] Pointers in TP4

pilgrimk@lafcol.UUCP (Pilgrim Kenwyn A) (11/18/88)

Is there any equivalent to the 

               ptr++ /* ptr = ptr + 1 */

in TP4. For example, in the following code segment:        

 var _Ptr: pointer;
 begin
 ....
  _Ptr := @SomeLocation;
 end;

How is it possible to move to the _Ptr+1 'th location?

-thanks

vanpelt@nvpna1.prl.philips.nl (bart van pelt) (11/28/88)

To increment pointers in TP4, use a type cast as follows

longint( Ptr ) := longint( Ptr ) + Increment;

ncsmith@ndsuvax.UUCP (Timothy Smith) (11/30/88)

In article <230@prles2.UUCP> vanpelt@nvpna1.UUCP (bart van pelt) writes:
>
>To increment pointers in TP4, use a type cast as follows
>
>longint( Ptr ) := longint( Ptr ) + Increment;

This is incorrect and will lead to possible large troubles.  TP4
depends on the offset of the pointer to be in the range of 0 to 15.
If your offsets do not conform to this rule then the results of any
conditional check between pointers is suspect.  Example: 0:16 and
1:0 will both point to the same location but if you use a conditional
operator such as '=' to compare them the test will fail even though both
pointers point to the same location.

The following equation should, I havn't tested it, work if you
want to do everything in one line.  It will only work when incrementing
a pointer.

longint(P) := (longint(P) and $FFFF0000) +
              ((longint(P) + inc) and $FFF0) shl 16 +
              ((longint(P) + inc) and $F)

The method that I prefer is as follows:

    type
        foo = record
            O: word;  { offset }
            S: word   { segment }
            end;

    procedure IncPtr( var Ptr: pointer;
                      N: word );
        var
            T: foo;
        begin
        T := foo(Ptr);
        inc(T.S, N div 16);
        inc(T.O, N mod 16);
        if T.O>15 then
            begin
            inc(T.S, T.O div 16);
            T.O := T.O mod 16
            end;
        Ptr := pointer(T)
        end;

    procedure DecPtr( var Ptr: pointer;
                      N: word );
        var
            T: foo;
        begin
        T := foo(Ptr);
        dec(T.S, N div 16);
        N := N mod 16;
        if N>T.O then
            begin
            dec(T.S);
            T.O := T.O + 16 - N
            end
        else
            dec(T.O, N mod 16);
        Ptr := pointer(T)
        end;

    I know that these procedures work as I implemented a K&R style
memory managment system with them.

    The reference to the offset range for pointers is on page 244
of the TP4 manual.

--
Tim Smith     North Dakota State University,  Fargo, ND  58105
UUCP:         ...!uunet!ndsuvax!ncsmith | 90% of the people on this planet
BITNET:       ncsmith@ndsuvax.bitnet    | are crazy and the rest of us are
INTERNET:     ncsmith@plains.NoDak.edu  | in grave danger of contamination

dmurdoch@watdcsu.waterloo.edu (D.J. Murdoch - Statistics) (11/30/88)

In article <230@prles2.UUCP> vanpelt@nvpna1.UUCP (bart van pelt) writes:
>
>To increment pointers in TP4, use a type cast as follows
>
>longint( Ptr ) := longint( Ptr ) + Increment;

No!  This won't work when you're crossing 64K segment boundaries.  It will
only increment the segment by 1, when it should be incremented by $1000.
Even though pointers use up 32 bits, addresses are only 20 bits long on a PC.

FINEBERG%WUMS.BITNET@cunyvm.cuny.edu (12/02/88)

To:     FINEBERG_C
Subj:   Re: Pointers in TP4

Received: From CUNYVM(MAILER) by WUMS with RSCS id 9650
          for FINEBERG_C@WUMS; Tue, 29-NOV-1988 04:01 CST
Received: from CUNYVM by CUNYVM.BITNET (Mailer X2.00) with BSMTP id 7537; Tue,
 29 Nov 88 04:51:22 EDT
Received: from VIM.BRL.MIL by CUNYVM.CUNY.EDU (IBM VM SMTP R1.1) with TCP; Tue,
 29 Nov 88 04:51:14 EDT
Received: from VIM.BRL.MIL by VIM.brl.MIL id aa15196; 29 Nov 88 1:36 EST
Received: from adm.brl.mil by VIM.BRL.MIL id aa15158; 29 Nov 88 1:22 EST
Received: from USENET by ADM.BRL.MIL id aa01166; 29 Nov 88 1:20 EST
Newsgroups: comp.lang.pascal
Subject: Re: Pointers in TP4
Message-ID: <230@prles2.UUCP>
Date: 28 Nov 88 08:36:27 GMT
Sender: nobody@prles2.uucp
Keywords: operations on them
To:       info-pascal@vim.brl.mil

bart van pelt <vanpelt@nvpna1.prl.philips.nl> writes:
> To increment pointers in TP4, use a type cast as follows
>
> longint( Ptr ) := longint( Ptr ) + Increment;
  ^^^^^^^^^^^^^^

Right idea but you shouldn't be able to assign ANYTHING to a function.
You can do it without using any conversion extensions as long as a pointer
type and integer type are the same length on your computer.  Just make a
pointer math type like this:

Type
  your_type_pointer = ^to_your_type;

  pointer_math  = record
    case boolean of
      Pnt       : your_type_pointer;
      IPnt      : integer;
  end { pointer_math record } ;

Procedure Bump_Pointer (Var P : your_type_pointer; HowMany : integer);

Var
  Cnvrtr        : pointer_math;

Begin { Bump_Pointer }
  Cnvrtr.Pnt := P;
  Cnvrtr.IPnt := Cnvrtr.IPnt + HowMany*SizeOfYourType;
  P := Cnvrtr.Pnt;
end { Bump_Pointer } ;

You would then call the routine Bump_Pointer(P,1) to go to the next element.
If TP4 has some size function (I.E. a function which will take as an argument
either a variable or type name and return the size in bytes of the data
type) then you should use that instead of the constant SizeOfYourType.
That way you don't have to sully yourself with dirty bytes and what not
;-).
        Charlie