Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm reading some Pascal code and everything is pretty straightforward except subranges. In the program I'm reading there's a variable d with the following type declaration:array[0..22] of 0..15; there's then the following procedure:

var a:integer;
  while k>0 do
      a:=(a+dig[k]*two) div 10;
  round_decimals:=(a+1) div 2;

Where k is a variable of type 0..63. So since k can be above the range of the index of dig how does this code work? — It's a routine for doing fixed point arithmetic and so I would assume the array indicie overflows in some controlled manner, but if anybody could explain the exact behavior I'd be really happy. Also, do Pascal arrays assign in some known manner? There were a lot of versions of Pascal and I think this program is written in a pretty “Pascal-version agnostic” manner, but I can't find much explanation on the subtle semantics of subrange expressions.

share|improve this question
decr(k)? What flavor of Pascal is this? I'm familiar with dec(), but not decr()... – Mason Wheeler May 21 '13 at 17:35
It's written in an intentionally-generic flavor of Pascal. decr(k) is just an alias for k := k - 1. I've modified the original, as you correctly point out that is obscure usage. Sorry about that. – veryfoolish May 21 '13 at 17:37
Since k is only decremented towards zero, it doesn't matter what its range is, so long as it starts below the top bound of the array being indexed. – Daniel R Hicks May 21 '13 at 20:25
The versions of Pascal I've worked with (don't ask me how long ago!!) did not strongly check the declared ranges of variables on assignment or use as an array index -- there might be a compiler warning but that was it. Rather, runtime checks were performed on the array index (unless disabled). – Daniel R Hicks May 21 '13 at 20:28
(Subranges were mostly useful as the basis of sets. Pascal had one of the best set implementations I've ever seen.) – Daniel R Hicks May 21 '13 at 20:29

It depends on the Pascal variant. In managed versions, like Oxygene or the DWS scripting language, assigning out of bounds of an array will always raise an exception at runtime. In Delphi and FPC, assigning out of bounds of an array should raise an exception at runtime, and does by default, but there's an option to turn bounds checking off via a compiler directive. The behavior of other Pascal compilers will vary, based on whether or not they generate bounds checking code.

share|improve this answer
I find this unfathomable because I just wrote a quick loop: begin for i:=0 to 63 do writeln(d[i]) end. and got no such runtime exceptions. – veryfoolish May 21 '13 at 17:45
@veryfoolish: What Pascal compiler are you using to test this? Does it support bounds checking? If so, is the support guaranteed or optional? If optional, do you have bounds checking on or off? – Mason Wheeler May 21 '13 at 18:17
I tried it in Free Pascal without any modifications... it gave me a bounds warning (not error[!]) when I tried to assign i > 63, but not when I looped i out of the 0..22 index range. – veryfoolish May 21 '13 at 18:38
@veryfoolish: FPC is a Delphi clone. It probably follows Delphi's example on this point. Try looking for an option for bounds checking in the project properties, and make sure to enable it. – Mason Wheeler May 21 '13 at 18:43
I am absolutely certain this code is correct, so I don't think you really answered my question. – veryfoolish May 21 '13 at 18:46

Figured it out; the ranges were chosen for alignment/offsets. Since Pascal (at least the old version of Pascal I'm reading) doesn't have short ints, etc. this was the alternative. Glad I don't write Pascal code. Anyway, it really had nothing to do with bounds checking and d[...] was pre-loaded in case anybody is wondering (I doubt it). Case closed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.