Recent

Author Topic: Please explain what is Pointer()?  (Read 1853 times)

Khrys

  • Full Member
  • ***
  • Posts: 148
Re: Please explain what is Pointer()?
« Reply #15 on: February 05, 2025, 07:41:21 am »
Out of curiosity, does anyone here know any architectures where  void* -> size_t -> void*  round-trips genuinely cannot work? The only example I can think of is 16-bit x86 with far pointers.

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Please explain what is Pointer()?
« Reply #16 on: February 05, 2025, 11:28:56 am »
void* is in pascal a procedure. It should not have a return value, so can not be chained. In C it also indicates that there is no valid return value.
We used to use some trickery in the past that we knew gave us a reliable return value but that is neither documented nor good practice. Apart from the short first sentence this only refers to C. void means empty, it is not a result.
That is on all C platforms. Has nothing to do with 16/32/64/128/whatever. You can not chain void* and if you do... because there is a value in a register... you are doing things very wrong.

So @Khrys, what makes you think otherwise?

That is bad C programming.

But this is a Pascal forum.
« Last Edit: February 05, 2025, 12:38:39 pm by Thaddy »
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 4994
Re: Please explain what is Pointer()?
« Reply #17 on: February 05, 2025, 01:11:52 pm »
@Thaddy,

I don't think @Khrys is talking about procedures or functions.

I believe he is talking about the amount of storage occupied by a pointer normally being the same as the integer type native size (NativeInt, size_t, ptrint, etc, depending on language and compiler.)

One well known exception is the one he mentioned, 16:16 far pointers.
 

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Khrys

  • Full Member
  • ***
  • Posts: 148
Re: Please explain what is Pointer()?
« Reply #18 on: February 06, 2025, 08:00:19 am »
@Thaddy,

I don't think @Khrys is talking about procedures or functions.

I believe he is talking about the amount of storage occupied by a pointer normally being the same as the integer type native size (NativeInt, size_t, ptrint, etc, depending on language and compiler.)

One well known exception is the one he mentioned, 16:16 far pointers.

You got it! I was probably too vague in my question.

I was asking about platforms where generic pointers cannot be converted to native-sized integers and then back to pointers because  SizeOf(Pointer) > SizeOf(PtrInt),  meaning that information gets lost in the conversion. In other words, I wanted to know on which platforms the warning "Conversion between ordinals and pointers is not portable" should turn into a hard error.

For example, on 16-bit x86 attempting to fit a far pointer into a native-sized integer would essentially be equivalent to this nonsense (I think):

Code: Pascal  [Select][+][-]
  1. type
  2.   TFarPointer = record
  3.     Segment, Offset: Word;
  4.   end;
  5.  
  6. var
  7.   PointerToItself: Word; // Native 16-bit word size; could also be PtrUInt (would still fail)
  8.  
  9. begin
  10.   PointerToItself:= TFarPointer(@PointerToItself); // Cannot convert structured type to ordinal
  11. end.

Warfley

  • Hero Member
  • *****
  • Posts: 1864
Re: Please explain what is Pointer()?
« Reply #19 on: February 06, 2025, 10:52:25 am »
Out of curiosity, does anyone here know any architectures where  void* -> size_t -> void*  round-trips genuinely cannot work? The only example I can think of is 16-bit x86 with far pointers.

Looking at the C standard section 7.22.1.4, it explicitly allows to cast pointers into type intptr_t/uintptr_t  and back, as long as the value is not changed in between.
It should be noted that this guarantee is solely given for these types, not for size_t or other int types, and also that the conversion to pointer is not guaranteed to work if you modify the pointer in between. All of that is undefined behavior.

So you are allowed to use (u)intptr_t to store pointer values, but you are not allowed to modify them or to use another int type to do so. At least according to the standard. If you need to do this, you must check the compiler manual for the target architecture if this is allowed.

But your explicit question, if you replace size_t with intptr_t, then the answer is: This is always allowed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5858
  • Compiler Developer
Re: Please explain what is Pointer()?
« Reply #20 on: February 06, 2025, 11:03:56 pm »
Out of curiosity, does anyone here know any architectures where  void* -> size_t -> void*  round-trips genuinely cannot work? The only example I can think of is 16-bit x86 with far pointers.

Just like you should use (u)intptr_t in C to solve this (as Warfley mentioned) in FPC you should use Ptr(U)Int and CodePtr(U)Int. It has always the same size as the corresponding Pointer or CodePointer type.

void* is in pascal a procedure.

C's void* in Pascal is just a Pointer. A procedure pointer in C would be void*() (note the parenthesis).

 

TinyPortal © 2005-2018