Recent

Author Topic: Difference between @ and Pointer ?  (Read 926 times)

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Difference between @ and Pointer ?
« on: July 20, 2025, 08:13:13 pm »
Hello,
like the Topic says: what is the Difference between:
@
and
Pointer ?
I was thinking that @ is the same as Pointer - only as short Form ...

I have the follow Code (which produce Error):
Error: Can't assign values to an address

Code: Pascal  [Select][+][-]
  1. function Foo: String;
  2. begin
  3.   ...
  4.   GetMem(@result, len + 1);
  5.   ...
  6. end;

Then I had replace @ with Pointer, and all will done okay:

Code: Pascal  [Select][+][-]
  1. function Foo: String;
  2. begin
  3.   ...
  4.   GetMem(Pointer(result), len + 1);
  5.   ...
  6. end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

440bx

  • Hero Member
  • *****
  • Posts: 6129
Re: Difference between @ and Pointer ?
« Reply #1 on: July 20, 2025, 08:20:07 pm »
@VarName returns the address where VarName is located.

pointer(VarName) does NOT return the address where VarName is located, it simply causes VarName to be treated as a pointer instead of whatever data type it was declared to be.  There are types that cannot be typecast to pointer, whereas, _any_ variable can be used with @

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6349
  • Compiler Developer
Re: Difference between @ and Pointer ?
« Reply #2 on: July 20, 2025, 09:32:24 pm »
Hello,
like the Topic says: what is the Difference between:
@
and
Pointer ?
I was thinking that @ is the same as Pointer - only as short Form ...

I have the follow Code (which produce Error):
Error: Can't assign values to an address

Code: Pascal  [Select][+][-]
  1. function Foo: String;
  2. begin
  3.   ...
  4.   GetMem(@result, len + 1);
  5.   ...
  6. end;

Then I had replace @ with Pointer, and all will done okay:

Code: Pascal  [Select][+][-]
  1. function Foo: String;
  2. begin
  3.   ...
  4.   GetMem(Pointer(result), len + 1);
  5.   ...
  6. end;

Your code is wrong in any way you spin it no matter whether the compiler compiles it or not.

First of, GetMem expects a value to assigned to as its first parameter. You can't change the value of an @-expression however. And using the cast either results in the first 4 or 8 Byte of the result variable to contain garbage (namely the address of the allocated memory) in case String is ShortString or to point to invalid string data in case String is AnsiString or UnicodeString, cause these managed types consist of more than “len + 1”.

So if you want to do this in a right way, either don't allocate anything at all if String is ShortString cause it will be passed as a whole 256 Byte anyway or use the variant of GetMem that takes a single parameter (the size) and returns the pointer and cast that to String and more importantly allocate len + SizeOf(TAnsiRec) or len + SizeOf(TUnicodeRec) depending on whether String is AnsiString or UnicodeString respectively.

More importantly I'd suggest you to simply let the RTL do its thing instead of trying to outsmart it.

 

TinyPortal © 2005-2018