delphipascalfreepascalturbo-pascal

From language POV, should Succ/Prev be applicable to pointers?


I wonder if Succ/Prev intrinsics should be able to be used over typed pointer types. Just like Inc/Dec and maths ( PointerVar+1 and PointerVar-1 ).

These only apply succ/pred to "ordinal types" which pointes are not listed a part of. So does Pascal Report 1972 (calling it Scalar Types)

However http://www.gnu-pascal.de/gpc/Succ.html#Succ claims "Application of Succ to pointers is defined in Borland Pascal." and it seems not reasonable to exclude those functions in the wake of Pointers Math.

Is this restriction substantiated language-wise or just an implementation problem, seeing Succ/Pred functions are seen as somewhat arcane?

program Project9;   // Delphi does have reverse-analogu for Pos/PosEx functions
{$APPTYPE CONSOLE}  // So Delphi IDE ( Version Insight) to cut away a last part
uses                // of string abuses mixing of record helper (LastIndexOf)
  System.SysUtils;  // and System.Copy function. Searchinf to fix it found this...
var
  OutPut, RemoteName: string;
  P: PChar;
begin
  try
    OutPut := 'aaaaaa/zzzzzz';
    P := StrRScan( PChar(OutPut), '/');

    P := Succ(P);
    // XE2: [DCC Fatal Error] Project9.dpr(13): F2084 Internal Error: AV0C068241-R00000000-0
    // 10.1: [dcc32 Error] Project9.dpr(13): E2008 Incompatible types

    P := 1+P;  // Another way to say Succ() - and works in both XE2 and 10.1
    Inc(P);    // Yet one more way to say Succ() - and works in both XE2 and 10.1 too    

    RemoteName := P;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Interesting to compare it with changed var type - P: Pointer; rather than PChar.

var P: Pointer; S: String;

P := Succ(P); // error
Inc(P);       // error
P := 1+P;     // works in XE2 if {$POINTERMATH ON}, error if {$POINTERMATH OFF}
              // error in 10.1 regardless

S := PChar(P);  // crashes XE2 if "P := 1+P;" is there above

Solution

  • Afaik TP does not allow any kind of increment, afaik not even on pchar (which Delphi before $pointermath already allowed). So the question is different for TP (segmented memory model!) as for Delphi.

    succ and pred are defined to operate on ordinals. Despite that you can add integers to pointers currently pointers are not considered an ordinal type. (see e.g. ordinal types).

    One could argue that it could be an ordinal type (a pointer in delphi adheres to the requirements in above link), but it doesn't as soon as your memory model is segmented (since there there are multiple minimums and maximums)

    An exception could be made, maybe, for succ and pred anyway, but what would be the point except a hope work? It doesn't make anything possible that couldn't be done before.