Take the 2-minute tour ×

I'm having a problem when I use these 2 FOR to initialize a two dimensional vector/array: I have these types defined:

  Range9         = 0..8;
  Digit         = '0'..'9';
  Board    = array [Range9,Range9] of Digit;

and then the part of the code where there are problems with the FOR's is the following:

  i : Range9;
  j : Range9;
  table : Board;

for i:=0 to 8 do begin
  for j:=0 to 8 do begin
    table[i,j] := '0'


Now the problem is that, when I debug this portion of code, for some reason, my i variable is modified when it's not supposed to.

For example, I have a watch on i and j and if I put a breakpoint in the line table[i,j] := 0 I see with the watches these values:

i    j
0    0
256  1
512  2
768  3
1024 4
1280 5
1536 6
1792 7
2048 8
2049 8
1    0
257  1
513  2
769  3

and so on...

So, when the program enters in the second for (the one that increases the j) my i increases in intervals of 256... I really don't know why is this happening.

And another thing I discovered is that, the problem solves if I change the TYPE of the i variable. If in the VAR section I put i : integer instead of i : Range9, i doesn't get modified when isn't supposed to.

I would really appreciate if someone explains me why is happening this.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I've found the answer to my own question... well, I didn't exactly found the answer, I've asked this same question in the forum board of the programming course I'm attending and one of the professors gave me this link:

(it's in spanish btw) http://www.fing.edu.uy/inco/cursos/prog1/pm/field.php/FAQ/Laboratorio#toc17

A quick translation:

This happens with variables defined as subranges. The reason isn't sure; but without doubt is an implementation error of the debugger. There is a 'trick' that can work to solve this (although not always), to be able to see the correct values on the debugger:

Suppose that you have the following variable in your program:

var anything: 1 .. 10;

Add in your program a integer variable which won't be used in any part of the program:

var anything: 1..10;
    aux: integer;  { only for the debugger }

Then when you define the debugger watch, instead of adding the anything variable, you should add the following expression:

aux:= anything

The aux variable can be used to view different variables, so you only need to declare one aux variable.

In some cases, the previous may not work. Another solution is to change the type of all the variables defined with subranges to integer, char, string, etc (depending the case) only for debug and the change it back again.

end of the translation.

Hope this will be useful for someone else facing the same error. BTW, this happens with the debugger of free pascal IDE 2.2.2 , maybe in another IDE/compiler/debugger of pascal it doesn't happen.

share|improve this answer
Glad to know you got your problem resolved! –  jrd1 Oct 8 '12 at 16:53

I haven't done Pascal in a while, so I might be a bit rusty. The only thing I can think of that is creating your problem is that you created a character range that was interpreted as a byte array, which was then converted to a Digits and then multiplied, which gave you those weird values. But, I could be wrong. I am unfamiliar with FreePascal.

    Range9 = 0..8
    Board = Array[Range9,Range9] of Integer;
A : Board;
I,J : Integer;
  For I:=0 to 8 do
    For J:=0 to 8 do

Reference: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf

share|improve this answer
But Range9 is the subrange of integer, only the integers (0,1,2,3,4,5,6,7,8) but still integers. In the only part I'm using char is in with table, how is possible that i is being modified in the second for? The even more weird thing is that, at the end of the execution of this block of code, I got my table array with all the elements set to '0'. So... maybe the problem is with the watches? –  mp19uy Oct 8 '12 at 16:18
As I said previously, I haven;t used Pascal in a while. So, I am unsure why you are getting that weird multiplication error. But, I know why all the elements in your table array are being set to 0. See this line: table[i,j] := '0'. That line is setting every value in the table to 0 as it iterates across the array. Ideally, you actually want this: table[i,j] := i*j, or something similar. –  jrd1 Oct 8 '12 at 16:26
Maybe I explained myself wrong. Yes, I want all the values of the elements of table set to '0'. That of the multiplication i*j, I don't know from where did you get that (Don't misunderstand me, I'm not being rude with last I said :D ) Anyways, I've found the answer about why happens that, is a problem of the debugger, in fact i has the correct values but the watch show it incorrectly. Thank you anyways for taking your time to help me. –  mp19uy Oct 8 '12 at 16:48

Your Answer


