Forum > General

$TYPEDADDRESS ON and pointer to pointer

(1/1)

440bx:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE      CONSOLE} {$TYPEDADDRESS      ON} program _PointerToPointer; type  PSOME_TYPE = ^TSOME_TYPE;  TSOME_TYPE = pointer;  function DoA(pp : PSOME_TYPE) : boolean;  { pp is a pointer to a pointer                        }begin  result := TRUE;end; function DoB(p : TSOME_TYPE) : boolean;  { p is just a pointer                                 }begin  result := TRUE;end; var  Avar : TSOME_TYPE;   p    : pointer; begin  Avar := nil;  p    := nil;   DoA(Avar);  { can this be made to require @ (@Avar) ? }  DoB(Avar);   DoA(@Avar);  DoB(@Avar);   DoA(p);     { can this be made to require @ (@p) ? }  DoB(p);   DoA(@p);  DoB(@p);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.

ccrause:

--- Quote from: 440bx on May 04, 2021, 08:22:10 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...
--- End quote ---
My first thought was to create a new type as follows:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  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.

marcov:
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).

440bx:
Thank you ccrause.


--- Quote from: ccrause on May 04, 2021, 11:07:29 am ---My first thought was to create a new type as follows:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  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.

--- End quote ---
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


--- Quote from: marcov on May 04, 2021, 11:35:51 am ---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).

--- End quote ---
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.

marcov:

--- Quote from: 440bx on May 04, 2021, 04:20:58 pm ---I was hoping that a pointer to a pointer was a "pointer to something" instead of just a plain pointer. 

--- End quote ---

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.

Navigation

[0] Message Index

Go to full version