Lazarus

Free Pascal => General => Topic started by: 440bx on May 04, 2021, 08:22:10 am

Title: $TYPEDADDRESS ON and pointer to pointer
Post by: 440bx on May 04, 2021, 08:22:10 am
Hello,

Even with TYPEDADDRESS ON the compiler does not make a distinction between a pointer and a pointer to a pointer.

Is there a way to tell the compiler to make a distinction (different types) between them.  In other words, in the following code snippet:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE      CONSOLE}
  2.  
  3. {$TYPEDADDRESS      ON}
  4.  
  5. program _PointerToPointer;
  6.  
  7. type
  8.   PSOME_TYPE = ^TSOME_TYPE;
  9.   TSOME_TYPE = pointer;
  10.  
  11.  
  12. function DoA(pp : PSOME_TYPE) : boolean;
  13.   { pp is a pointer to a pointer                        }
  14. begin
  15.   result := TRUE;
  16. end;
  17.  
  18. function DoB(p : TSOME_TYPE) : boolean;
  19.   { p is just a pointer                                 }
  20. begin
  21.   result := TRUE;
  22. end;
  23.  
  24. var
  25.   Avar : TSOME_TYPE;
  26.  
  27.   p    : pointer;
  28.  
  29. begin
  30.   Avar := nil;
  31.   p    := nil;
  32.  
  33.   DoA(Avar);  { can this be made to require @ (@Avar) ? }
  34.   DoB(Avar);
  35.  
  36.   DoA(@Avar);
  37.   DoB(@Avar);
  38.  
  39.   DoA(p);     { can this be made to require @ (@p) ? }
  40.   DoB(p);
  41.  
  42.   DoA(@p);
  43.   DoB(@p);
  44. end.            
I would like the compiler to reject the "DoA(Avar)" because DoA requires a pointer to a pointer, not just a plain pointer.

Thank you for your help.
Title: Re: $TYPEDADDRESS ON and pointer to pointer
Post by: ccrause on May 04, 2021, 11:07:29 am
Even with TYPEDADDRESS ON the compiler does not make a distinction between a pointer and a pointer to a pointer.

Is there a way to tell the compiler to make a distinction (different types) between them...
My first thought was to create a new type as follows:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSOME_TYPE = type pointer;

However this doesn't solve your problem because TSOME_TYPE is still assignment compatible with an untyped pointer itself.  I think the real problem for your use case is that a pointer and a pointer to a pointer is identical from the compiler's perspective and even if you try to create a new type deriving from pointer it will be assignment compatible.
Title: Re: $TYPEDADDRESS ON and pointer to pointer
Post by: marcov on May 04, 2021, 11:35:51 am
As far as I know this:

Typed address mode means that @x generates "pointer to x" as type. "Pointer" is compatible with every pointer type and that doesn't change in Typed address mode.

If you don't want confusing type conversions, don't use "pointer", but always "pointer to something".

A classic trick is to define it as pointer to byte or an empty record (in the latter case sizeof=0 then sets it clearly apart).
Title: Re: $TYPEDADDRESS ON and pointer to pointer
Post by: 440bx on May 04, 2021, 04:20:58 pm
Thank you ccrause.

My first thought was to create a new type as follows:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSOME_TYPE = type pointer;

However this doesn't solve your problem because TSOME_TYPE is still assignment compatible with an untyped pointer itself.  I think the real problem for your use case is that a pointer and a pointer to a pointer is identical from the compiler's perspective and even if you try to create a new type deriving from pointer it will be assignment compatible.
As you pointed out, assignment compatibility seems to get in the way of differentiating between a pointer and a pointer to a pointer.



Thank you marco

If you don't want confusing type conversions, don't use "pointer", but always "pointer to something".

A classic trick is to define it as pointer to byte or an empty record (in the latter case sizeof=0 then sets it clearly apart).
I was hoping that a pointer to a pointer was a "pointer to something" instead of just a plain pointer. 



Good to know the problem isn't caused by some oversight on my part.
Title: Re: $TYPEDADDRESS ON and pointer to pointer
Post by: marcov on May 04, 2021, 07:59:08 pm
I was hoping that a pointer to a pointer was a "pointer to something" instead of just a plain pointer. 

It is, but still compatible with pointer, so you can't make a difference between passing a ppointer or a pointer.

Note that this is just my gut feeling what is happening, I assume Pascaldragon will answer it more indepth.
TinyPortal © 2005-2018