Wtf? How can @P != @P[0].. I see what you guys are saying and yes using @P[0] works but seriously?!
It makes absolutely no sense to have the ebp point to a random location for heap arrays and to base[0] for stack arrays. The assembly is borked?
That's like having a heap array start at index 1 and stack arrays at 0.
K when I get home from work ill edit that tutorial.. It's weird though because the code in the tutorial still works and off the top of my head, I think it is using @Ptr and not @Ptr[0]. Ill have to check again.
I also remember using the same memset function using an @stringvariablehere and it worked just fine.
However, nice discovery and tests :) There are side effects to this though..
Procedure MemSet(Data: Pointer; Value: Byte; Size: Integer);
var
Ptr: ^Byte;
Begin
For Ptr := Data To Data + Size Do
Ptr^ := Value;
End;
var
p: array of byte;
ptra, ptrb: Pointer;
begin
setlength(p, 10);
ptra := @p;
ptrb := @p[0];
writeln(Uint32(ptra));
writeln(Uint32(ptrb));
writeln(Uint32(@P) - Uint32(@P[0])); //Useless.. P[0] is at random offset from @P.
writeln(Uint32(@P[1] - Uint32(@P[0])); //offset of 1 byte as expected.
end;
And in the following, you'll notice that it works just fine using @StringVariable instead of @StringVariable[0] which does NOT work even though a string is an array of bytes/chars:
Procedure MemSet(Data: Pointer; Value: Byte; Size: Integer);
var
Ptr: ^Byte;
Begin
For Ptr := Data To Data + Size Do
Ptr^ := Value;
End;
var
c : Array of Char;
s : string;
i : integer;
begin
c := ['h', 'e', 'l', 'l', 'o'];
s := '';
writeln(length(c));
setlength(s, length(c));
//MemSet(@s, 0, length(c)); //uncomment this line and you'll see the difference..
MemSet(@s[0], 0, length(c)); //Lol.. fail..
for i := 0 to sizeof(c) do
begin
s := s + c[i];
end;
writeln(s);
end.
And finally the one you guys already figured out(absolutely ridiculous):
Procedure MemSet(Data: Pointer; Value: Byte; Size: Integer);
var
Ptr: ^Byte;
Begin
For Ptr := Data To Data + Size Do
Ptr^ := Value;
End;
var
Ptr: ^Integer;
TIA: array[0..4] of integer;
TDIA: array of integer;
begin
TIA := [1, 2, 3, 4, 5];
TDIA := [1, 2, 3, 4, 5];
writeln('Printing stack array');
Ptr := @TIA; //Does not need indexing..
For 0 To High(TIA) do
begin
writeln(Ptr^);
Inc(Ptr);
end;
writeln(#13#10 + 'Printing heap array');
Ptr := @TDIA[0]; //Needs indexing..
For 0 To High(TIA) do
begin
writeln(Ptr^);
Inc(Ptr);
end;
end;
Just makes me wanna stop using Lape tbh.. so many things wrong..