View Issue Details

IDProjectCategoryView StatusLast Update
0037578FPCDocumentationpublic2020-08-17 11:10
ReporterKai Burghardt Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platformx86_64OSGNU/Linux 
Product Version3.0.4 
Fixed in Version3.3.1 
Summary0037578: Programmer’s guide $pointerMath directive affects data type definitions
DescriptionIncorporate note from

https://wiki.freepascal.org/User_Changes_2.6.0#The_POINTERARITHMETICS_mode_switch_has_been_replaced_with_the_POINTERMATH_directive
Steps To Reproduceprogram pointerArithmetics(input, output, stdErr);
{$pointerMath off}
var
    // PByte was defined by system unit
    // while {$pointerMath on}
    p: PByte;
    
    // Let’s define a “new” PByte:
    l: ^Byte;
begin
    // everything fine:
    p := nil;
    p := p + 42;
    
    l := nil;
    // This works, too, but I don’t know why exactly:
    inc(l);
    // But this won’t work:
    //l := l + 7;
end.
TagsNo tags attached.
Fixed in Revision1732
FPCOldBugId
FPCTarget3.2.2
Attached Files

Activities

Kai Burghardt

2020-08-15 20:15

reporter  

prog.tex.patch (485 bytes)   
--- prog.tex~	2020-08-15 18:10:11.542185324 +0000
+++ prog.tex	2020-08-15 18:10:11.542185324 +0000
@@ -2398,6 +2398,12 @@
 tells the compiler to give an error whenever pointer math appears in an
 expression.
 
+\begin{remark}
+All types declared while POINTERMATH was on
+support pointer arithmetic expressions.
+This includes types defined by the system unit,
+such as \var{Pointer}, \var{PChar} and \var{PInteger}.
+\end{remark}
 
 \subsection{\var{\protect\$PROFILE} : Profiling}
 
prog.tex.patch (485 bytes)   

Sven Barth

2020-08-16 14:20

manager   ~0124920

Is it on purpose that the "Steps to reproduce" contains the example twice? Or am I just missing the difference between the two?

Also: Inc and Dec can be used with pointer types, because they were what was originally used with pointer types to increment/decrement them before pointer arithmetics were introduced (they already worked back in TP times and are probably as old as typed pointers).

nanobit

2020-08-16 16:46

reporter   ~0124924

AFAIR, Pascal has two support degrees (reporter needs to re-check):
1) Basic pointer-math ({$X+} required). Supported pointer-types:
pByte, pAnsiChar, pWideChar, pWord, pInteger, pDouble; pPointer; but no structure types
2) Extended pointer-math (works only under {$pointermath on})
adds pointer-types for records and static arrays

Kai Burghardt

2020-08-16 21:21

reporter   ~0124931

No, Sven, there’s no difference between the example code. I must have accidentally hit the middle mouse button twice (“paste” X11 selections), but I didn’t notice that since the text field is so small.

I understood inc/dec weren’t real procedures, but compiler intrinsics, so virtually a shorthand for destination := destination ± 1 or sizeOf(destination^) which wouldn’t constitute a legal expression if {$pointerMath off}.

Nanobit, I can’t confirm your claim $extendedSyntax, or $X for short, had anything to do with pointer arithmetics.

Serge Anvarov

2020-08-16 21:43

reporter   ~0124932

I think it is better to specify in the FPC documentation for Inc and Dec procedures the same as in the Delphi documentation http://docwiki.embarcadero.com/Libraries/Rio/en/System.Inc : "If X is a pointer type, it increments X by N times the size of the type pointed to". Regardless of the $POINTERMATH state (Delphi compatible).

Kai Burghardt

2020-08-16 23:29

reporter   ~0124937

system.xml.patch (1,576 bytes)   
--- system.xml~	2020-08-16 21:28:33.631530341 +0000
+++ system.xml	2020-08-16 21:28:33.631530341 +0000
@@ -948,8 +948,17 @@
 <element name="Dec">
 <short>Decrease value of variable</short>
 <descr>
+<p>
 <var>Dec</var> decreases the value of <var>X</var> with <var>Decrement</var>.
-If <var>Decrement</var> isn't specified, then 1 is taken as a default.
+If <var>X</var> is a typed pointer,
+<var>Decrement</var> denominates the number of the domain type’s values to skip.
+The domain type’s size is automatically taken account of.
+If <var>Decrement</var> is not specified, then 1 is taken as a default.
+</p>
+<p>
+<em>Note:</em> <var>Dec</var> works with pointers
+regardless of the <var>{$pointerMath}</var> compiler switch state.
+</p>
 </descr>
 <errors>
 A range check can occur, or an underflow error, if an attempt is made
@@ -1581,8 +1590,17 @@
 <element name="Inc">
 <short>Increase value of integer variable</short>
 <descr>
+<p>
 <var>Inc</var> increases the value of <var>X</var> with <var>Increment</var>.
-If <var>Increment</var> isn't specified, then 1 is taken as a default.
+If <var>X</var> is a typed pointer,
+<var>Increment</var> denominates the number of the domain type’s values to skip.
+The domain type’s size is automatically taken account of.
+If <var>Increment</var> is not specified, 1 is assumed as a default.
+</p>
+<p>
+<em>Note:</em> <var>Inc</var> works with pointers
+regardless of the <var>{$pointerMath}</var> compiler switch state.
+</p>
 </descr>
 <errors>
 If range checking is on, then A range check can occur, or an overflow
system.xml.patch (1,576 bytes)   

nanobit

2020-08-17 08:13

reporter   ~0124939

Under FPC3.2: If I place global {$X-}, then math for basic pointer types is disabled
p2 := pbyte( p) + k; // Error: Operation "+" not supported for types "PByte" and "LongInt"
p2 := pdouble( p) + k; // Error: Operation "+" not supported for types "PDouble" and "LongInt"

Sven Barth

2020-08-17 09:43

manager   ~0124943

@Kai: I've removed the duplicated example. Also, while yes, Inc and Dec are intrinsics and not simple functions they are not a mere drop in replacement for "X := X + 1" or "X := X - 1" as they were already used back in TP times to generate more optimal code (don't forget that TP didn't have that many optimizations, thus Inc(X) became an INC X compared to a ADD X, 1). For FPC the differences are indeed fewer due to it providing more optimizations.

@nanobit: Yes, disabling extended syntax disables pointer arithmetic as well. Should probably be documented for $X...

Michael Van Canneyt

2020-08-17 11:10

administrator   ~0124944

Added some remarks to pointermath and to the inc/dec routines.

Issue History

Date Modified Username Field Change
2020-08-15 20:15 Kai Burghardt New Issue
2020-08-15 20:15 Kai Burghardt Status new => assigned
2020-08-15 20:15 Kai Burghardt Assigned To => Michael Van Canneyt
2020-08-15 20:15 Kai Burghardt File Added: prog.tex.patch
2020-08-16 14:20 Sven Barth Note Added: 0124920
2020-08-16 16:46 nanobit Note Added: 0124924
2020-08-16 21:21 Kai Burghardt Note Added: 0124931
2020-08-16 21:43 Serge Anvarov Note Added: 0124932
2020-08-16 23:29 Kai Burghardt Note Added: 0124937
2020-08-16 23:29 Kai Burghardt File Added: system.xml.patch
2020-08-17 08:13 nanobit Note Added: 0124939
2020-08-17 09:31 Sven Barth Description Updated View Revisions
2020-08-17 09:31 Sven Barth Steps to Reproduce Updated View Revisions
2020-08-17 09:31 Sven Barth FPCTarget => -
2020-08-17 09:43 Sven Barth Note Added: 0124943
2020-08-17 11:10 Michael Van Canneyt Status assigned => resolved
2020-08-17 11:10 Michael Van Canneyt Resolution open => fixed
2020-08-17 11:10 Michael Van Canneyt Fixed in Version => 3.3.1
2020-08-17 11:10 Michael Van Canneyt Fixed in Revision => 1732
2020-08-17 11:10 Michael Van Canneyt FPCTarget - => 3.2.2
2020-08-17 11:10 Michael Van Canneyt Note Added: 0124944