Recent

Author Topic: Range Checking  (Read 7848 times)

graemejc

  • New Member
  • *
  • Posts: 33
Range Checking
« on: January 24, 2019, 02:45:21 pm »
This is not so much a help-me-please-question, rather it's a do-you-think-it-strange-too-question?

I recently wrote code which gave me a strange result and I realised that the reason was that I had copied code to manipulate a 9 element array for a different purpose and I only needed a 6 element array.

In trying to print out the 6 element array, I was accidentally trying to print 9 elements, and as no error was generated, it must be that no range checking was done. So searched the documentation and discovered that {$R+} or {$RangeChecks ON} will generate range checking code.

And that {$R-} or {$RangeChecks OFF} will disable generation of range checking code.

So, do-you-think-it-strange-too that the default is range checking OFF?

Or am I just showing my age and that my last regular programming experience was late 80s early 90s when range checking was an important part of Pascal.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Range Checking
« Reply #1 on: January 24, 2019, 03:30:03 pm »
The default modes, both for Lazarus and FP/FPC, are a compromise between what could be termed "heavy debugging" and "full release" modes, so it adds a little debugging info but doesn't sets on the full-checking "artillery".

And if you're showing your age, I guess most of us do: I was also a little startled when I discovered it. :)

Do note, though, that the default configuration for Turbo Pascal also had range checking turned off:
Code: [Select]
Borland Pascal  Version 7.0  Copyright (c) 1983,92 Borland International
[... compiler options ...]
Compiler switches: -$<letter><state>  (defaults are shown below)
  A+ Word alignment       K+ Smart callbacks      S+ Stack checking
  B- Full boolean eval    L+ Local debug symbols  T- Typed pointers
  D+ Debug information    N- 80x87 instructions   V+ Strict var-strings
  E+ 80x87 emulation      O- Overlays allowed     W+ Windows stack frames
  F- Force FAR calls      P- Open string params   X+ Extended syntax
  G- 80286 instructions   Q- Overflow checking    Y+ Symbol reference info
  I+ I/O error checking   R- Range checking
Memory sizes: -$M<stack>,<heapmin>,<heapmax>  (default: 16384,0,655360)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Range Checking
« Reply #2 on: January 24, 2019, 03:37:28 pm »
There are many occasions when you definitely want no range checking.
For instance when you are doing a typecast for which the compiler would flag a range error (the compiler has to assume a worst case scenario) but which you know -- given the data range your program handles, of which the compiler is unaware -- is a perfectly safe typecast.
Or for production code of tested and debugged routines which should not be slowed down and bloated by unnecessary range checking code.

So the default is a sensible choice for such common situations. In the development phase of new software you are stupid, of course, to omit range checking. But all it takes in Lazarus is one tick in a checkbox on the debugging page of your project options. Great safety gain for minimal effort.
« Last Edit: January 24, 2019, 10:10:44 pm by howardpc »

Kays

  • Hero Member
  • *****
  • Posts: 632
  • Whasup!?
    • KaiBurghardt.de
Re: Range Checking
« Reply #3 on: January 25, 2019, 02:46:31 pm »
[…] I was accidentally trying to print 9 elements, and as no error was generated, it must be that no range checking was done.
That's (also) why you always use high() (and low() [in conjunction with non-dynamic arrays]).

So, do-you-think-it-strange-too that the default is range checking OFF? […]
At first I scratched my head, too. It is clearly in violation with the relevant ISO standards:
Quote
A value of type T2 shall be designated assignment-compatible with a type T1 if […] T1 and T2 are compatible ordinal-types, and the value of type T2 is in the closed interval specified by the type T1. […]

[…] it shall be an error if T1 and T2 are compatible ordinal-types and the value of type T2 is not in the closed interval specified by the type T1;

I agree with the point, that range checks cost some time, time you don't necessarily always have to spend, since you know it'll be within the range. But I guess, it's within the permission to omit error detection where it would be, quote unquote, “an excessive burden.”
Yours Sincerely
Kai Burghardt

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12011
  • Debugger - SynEdit - and more
    • wiki
Re: Range Checking
« Reply #4 on: January 25, 2019, 03:00:46 pm »
Off course there are merits to turn it on or off, or even have the option to do so.... But that is not the question.

The question is what should be an expected default.
And IMHO that should simply be consistent with other settings. I would say for a debugging set-up, range check should be an included default.
For a release setting this may not be as straight forward. But I would say off.

I don't know what currently the defaults are, an a freshly installed Lazarus.
Nor what they are after you created debug and release build modes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6263
  • Compiler Developer
Re: Range Checking
« Reply #5 on: January 25, 2019, 04:15:06 pm »
So, do-you-think-it-strange-too that the default is range checking OFF? […]
At first I scratched my head, too. It is clearly in violation with the relevant ISO standards:
Quote
A value of type T2 shall be designated assignment-compatible with a type T1 if […] T1 and T2 are compatible ordinal-types, and the value of type T2 is in the closed interval specified by the type T1. […]

[…] it shall be an error if T1 and T2 are compatible ordinal-types and the value of type T2 is not in the closed interval specified by the type T1;
Turbo Pascal, Delphi and the non-ISO modes of FPC have no obligation to be ISO compatible, in fact the FPC mode leans towards compatibility with Turbo Pascal, while the ObjFPC mode leans towards compatiblity with Delphi which both don't have such checks enabled by default either.

Kays

  • Hero Member
  • *****
  • Posts: 632
  • Whasup!?
    • KaiBurghardt.de
Re: Range Checking
« Reply #6 on: January 25, 2019, 06:51:46 pm »
[…] Turbo Pascal, Delphi and the non-ISO modes of FPC have no obligation to be ISO compatible, […]
Of course not, but turning off range checks in general (per distribution) just doesn't align with Pascal's paradigm to prevent the programmer doing stupid things. It's the same issue with {$boolEval}.

PS: And {$mode extendedPascal}. Both {$mode ISO} and {$mode extendedPascal} should conform to their corresponding ISO standard.
Yours Sincerely
Kai Burghardt

graemejc

  • New Member
  • *
  • Posts: 33
Re: Range Checking
« Reply #7 on: January 28, 2019, 11:35:41 am »
The default modes, both for Lazarus and FP/FPC, are a compromise between what could be termed "heavy debugging" and "full release" modes, so it adds a little debugging info but doesn't sets on the full-checking "artillery".

And if you're showing your age, I guess most of us do: I was also a little startled when I discovered it. :)

Do note, though, that the default configuration for Turbo Pascal also had range checking turned off:
Code: [Select]
Borland Pascal  Version 7.0  Copyright (c) 1983,92 Borland International
[... compiler options ...]
Compiler switches: -$<letter><state>  (defaults are shown below)
  A+ Word alignment       K+ Smart callbacks      S+ Stack checking
  B- Full boolean eval    L+ Local debug symbols  T- Typed pointers
  D+ Debug information    N- 80x87 instructions   V+ Strict var-strings
  E+ 80x87 emulation      O- Overlays allowed     W+ Windows stack frames
  F- Force FAR calls      P- Open string params   X+ Extended syntax
  G- 80286 instructions   Q- Overflow checking    Y+ Symbol reference info
  I+ I/O error checking   R- Range checking
Memory sizes: -$M<stack>,<heapmin>,<heapmax>  (default: 16384,0,655360)

Thanks for that, especially the information re: the other modes. Important to know.


Is Pascal still taught at universities? To the best of my knowledge, in Australia, no. If not, does that mean that most on this forum, using freePascal are "oldies."

dbannon

  • Hero Member
  • *****
  • Posts: 3646
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Range Checking
« Reply #8 on: January 28, 2019, 12:54:50 pm »
Is Pascal still taught at universities?
Not that I know of (in Oz). RMIT was using in in the 1990's

Mind you, we do see a number of questions asked, from time to time, that pretty obviously relate to first year assignments ......
 
To the best of my knowledge, in Australia, no. If not, does that mean that most on this forum, using freePascal are "oldies."
Guilty as accused Your Honour !
Davo
« Last Edit: January 28, 2019, 01:03:53 pm by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

PascalDragon

  • Hero Member
  • *****
  • Posts: 6263
  • Compiler Developer
Re: Range Checking
« Reply #9 on: January 28, 2019, 02:43:43 pm »
[…] Turbo Pascal, Delphi and the non-ISO modes of FPC have no obligation to be ISO compatible, […]
Of course not, but turning off range checks in general (per distribution) just doesn't align with Pascal's paradigm to prevent the programmer doing stupid things. It's the same issue with {$boolEval}.
Enabling these options by default in the modes that provide compatibility for TP and Delphi code is simply inviting unnecessary bug reports of users that complain that their code compiles/runs in TP/Delphi without any problems.

PS: And {$mode extendedPascal}. Both {$mode ISO} and {$mode extendedPascal} should conform to their corresponding ISO standard.
Feel free to submit bug reports if those two modes don't adhere to their corresponding standard.

 

TinyPortal © 2005-2018