Strange behaviour of strings when messing with pointers
Original Reporter info from Mantis: msx80
-
Reporter name:
Original Reporter info from Mantis: msx80
- Reporter name:
Description:
I was developing a simple circular buffer and using ansistrings to make some tests.
I used @s[1] to access the buffer of a 4 char string (which should be synonimous with @s, shouldn't it?), making some read and write.
At one point a string assignement seems to get lost.
I know that's not the most correct way to do this stuff, but the compiler should be coherent in this cases too.
Replacing @s[1] with @s fixes the problem.
The program is pasted below (the smallest i could get).
Additional information:
program ohbug;
{$H+}
uses
Classes, SysUtils;
// ** simple linear buffer implementation
var buffer:pointer;
head:integer=0;
tail:integer=0;
procedure read(dest:pointer; size:integer);
begin
move( (buffer+tail)^, dest^, size );
inc(tail, size);
end;
procedure write(dest:pointer; size:integer);
begin
move( dest^, (buffer+head)^, size );
inc(head, size);
end;
// **
var s:string;
begin
buffer:=GetMem(10000); // enough for us
// some read and write...
s:='abcd';
Write(@s[1], 4);
s:='1234';
Write(@s[1], 4);
read(@s[1], 4);
writeln(s);
s:='EFGH';
Write(@s[1], 4);
read(@s[1], 4);
writeln(s);
read(@s[1], 4);
writeln(s);
// some read and write...
s:='abcd';
Write(@s[1], 4);
s:='1234'; // <-- is this lost ?
writeln('this should print 1234, instead prints abcd! ---> ', s);
Write(@s[1], 4);
read(@s[1], 4);
writeln(s);
s:='EFGH';
Write(@s[1], 4);
read(@s[1], 4);
writeln(s);
read(@s[1], 4);
writeln(s);
end.
Mantis conversion info:
- Mantis ID: 10341
- Version: 2.2.0