There was a very strange error invalid pointer operation, the frequency of its occurrence is even stranger. There is a code:

for i := 0 to (fpos div length(longke)) do longkey := longkey + longke; for i:= 0 to fpos do AByte[i]:= AByte[i] xor ord(longkey[i+1]); 

noticed that if you remove the cycle

 for i := 0 to (fpos div length(longke)) do longkey := longkey + longke; 

then there is no error, just by typing, I realized that if I noted a line

 AByte[i]:= AByte[i] xor ord(longkey[i+1]); 

then there is no error either, so it’s obviously a deal with her. The very same error occurs at the end of the program and shows

  Application.CreateForm(TForm1, Form1); Application.Run; end. // сюда 

sometimes the error goes away itself, I didn’t touch anything, I didn’t change the code, and suddenly the error stopped appearing, but then it started again, what kind of magic?

Addition # 1: I realized that if we declare an array static, there is no error, here’s the code:

 var f: file of byte; AByte: array of byte; bytee : byte; i,k,fpos,sc,vv,pol :integer; CodeText : longint; longke,longkey: AnsiString; begin longke:='c28a50a73a5ccc2373c1015e41022df4'; AssignFile(f, 'C:\45.docx'); Filemode := 2; Reset(f); k := FileSize(f); fpos:=200; SetLength(AByte, fpos); for i := 0 to (fpos div length(longke)) do longkey := longkey + longke; seek(f,0); BlockRead(f, AByte[0], fpos); for i:= 0 to fpos do AByte[i]:= AByte[i] xor ord(longkey[i+1]); seek(f,0); BlockWrite(f, AByte[0], fpos); end; 
  • Well, if you think about it - on the last turn of the cycle, where will i + 1 point to? And not necessarily on the last - kami
  • This is not an error, for I recently added, it was just i, and in longkey there were more characters than in fpos - Lolidze
  • one
    a little bit wrong. Edit the question. And besides the declaration, show where you initialize the variables. In general, we need a minimal reproducible example. - kami
  • 2
    @Lolidze There is no "no error", but "You do not see it." Error due to violation of array boundaries. More detail can be said only after seeing the whole code. Turn on the Range Check option in the compiler settings and see what happens. Well, about FastMM you have already been told - Anton Shchyrov
  • one
    From what is now - an error in the cycle by AByte. The cycle must go to fpos-1, otherwise it will take place 201 times instead of 200. Well, the length of the longkey is clearly insufficient. - kami

1 answer 1

SetLength(AByte, fpos);

Sets an AByte array AByte size of fpos elements. Indexing items from 0 to fpos - 1

for i:= 0 to fpos do AByte[i]:= AByte[i] xor ord(longkey[i+1]);

At the last iteration of the loop, the AByte array AByte with the index fpos . There is no element with this index and we are overwriting someone else's memory.