0
votes

My code works but keeps giving me access violation error. " Access violation at address 00440690B in module. read of address 01F62C42." what is wrong? and how can I make it work? The second loop does nothing. please help!

Var
   num1, num2, k : Integer;
   LL : string;

begin

LL := '       ';
num1 := 4;
num2 := 4;
  for k := 1 to 7 do
    begin
      LL[num1] := '*';
      LL[num2] := '*';
      redt.Lines.Add(LL);
      num1 := num1 +1;
      num2 := num2 -1;
    end;
  for k := 1 to 3 do
redt.Lines.Add('   *   ');

end;
1
"My code works but keeps giving me access violation error." - That sentence does not make any sense, logically. If your code would work, it would not throw the AV. If throwing the AV is by design, you would not ask that question. Hence, the code is not working, obviously. - JensG
thanks. I changed the "1 to 7" to "1 to 4" and it worked. thank you :) - GerhardAA

1 Answers

11
votes

My code works.

No, it does not. You are accessing elements of LL that are out-of-bounds. In the final iteration of the first loop, num1 has value 10, and num2 has value -2. Both of these are out-of-bounds when used as indices for LL. Valid indices for LL are 1 to 7. So I guess that the first loop should run for 1 to 4.

If you would enable range checking in the compiler options, the compiler would be able to tell you this. I cannot stress enough the importance of using range checking. Use it, and let the compiler find your defects.