Recent

Author Topic: equivalent to C/C++ "offsetof" ?  (Read 792 times)

jamie

  • Hero Member
  • *****
  • Posts: 7902
equivalent to C/C++ "offsetof" ?
« on: July 11, 2026, 02:32:18 am »
I used to be able to do something like this:

     UintPtr(@TSomeRecord(Nil).AmemberField);

I could of use "Integer" as the cast instead, can't remember but, it will give me the offset value of the field.

Jamie
The only true wisdom is knowing you know nothing

Fibonacci

  • Hero Member
  • *****
  • Posts: 1081
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Re: equivalent to C/C++ "offsetof" ?
« Reply #1 on: July 11, 2026, 02:44:35 am »
Code: Pascal  [Select][+][-]
  1. writeln(UintPtr(@TSomeRecord(Nil^).AmemberField)); // objfpc
  2. writeln(OffsetOf(TSomeRecord.AmemberField));       // unleashed  
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: equivalent to C/C++ "offsetof" ?
« Reply #2 on: July 11, 2026, 12:58:11 pm »
Looks interesting. The unleased FPC compiler, that is.
The only true wisdom is knowing you know nothing

creaothceann

  • Sr. Member
  • ****
  • Posts: 428
Re: equivalent to C/C++ "offsetof" ?
« Reply #3 on: July 12, 2026, 12:50:49 am »
*unleashed

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: equivalent to C/C++ "offsetof" ?
« Reply #4 on: July 12, 2026, 02:43:16 pm »
*unleashed

As Elvis Presley would say! "Thank you, Thank you very much" :D

Jamie
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7902
Re: equivalent to C/C++ "offsetof" ?
« Reply #5 on: July 14, 2026, 12:27:52 am »
Code: Pascal  [Select][+][-]
  1. writeln(UintPtr(@TSomeRecord(Nil^).AmemberField)); // objfpc
  2. writeln(OffsetOf(TSomeRecord.AmemberField));       // unleashed  

 I am kind of curious as about that "offsetof", its a compiler intrinsic? what I mean by that, can it evaluate values at compile time when building Enumerators and sets?

Example
Code: Pascal  [Select][+][-]
  1. Const
  2.   Enums:Array[0..4] of Integer = (OffsetOf(TRect.Left),offsetOf(TRect.top),offsetOf(TRrect.Right),offsetOf(TRect.Bottom), 10);
  3.  
  4. //Etc.
  5.  

The same would hold true for defining enum types etc.

Jamie
The only true wisdom is knowing you know nothing

Fibonacci

  • Hero Member
  • *****
  • Posts: 1081
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Re: equivalent to C/C++ "offsetof" ?
« Reply #6 on: July 14, 2026, 03:40:37 am »
I am kind of curious as about that "offsetof", its a compiler intrinsic? what I mean by that, can it evaluate values at compile time when building Enumerators and sets?

Yes - it's a compiler intrinsic that const-folds to a plain integer at compile time, so it works anywhere a constant expression is allowed. It's part of the Composable Records feature - OffsetOf() and BitOffsetOf().

Code: Pascal  [Select][+][-]
  1. const
  2.   Enums: Array[0..4] of Integer = (OffsetOf(TRect.Left),offsetOf(TRect.top),offsetOf(TRect.Right),offsetOf(TRect.Bottom), 10);
  3.  
  4. begin
  5.   for var i := 0 to high(Enums) do writeln('Enums[', i, '] = ', Enums[i]);

Code: Text  [Select][+][-]
  1. Enums[0] = 0
  2. Enums[1] = 4
  3. Enums[2] = 8
  4. Enums[3] = 12
  5. Enums[4] = 10

Give it (Unleashed) a try - you might find you like it ;) Install is super simple: use the installer, or fpcupdeluxe after adding 2 lines to the INI. It's recently synced with upstream FPC, with loads of features on top. And if you think this particular feature (these intrinsics) would be useful in stock FPC, go ahead and open a feature-request issue on GitLab - the code's ready, and the FPC team is free to take any part of it into stock FPC if they want.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: equivalent to C/C++ "offsetof" ?
« Reply #7 on: July 14, 2026, 01:15:41 pm »
I can confirm everybody should try it and the installation is a doddle.
I hesitate to use it for production code, though.
Features like above are strong additions as is some of the Warfeley code that are dangling merge requests in main and committed in unleashed. These could be committed in main as-is.
« Last Edit: July 14, 2026, 01:19:33 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: equivalent to C/C++ "offsetof" ?
« Reply #8 on: July 14, 2026, 07:37:23 pm »
But unleashed has the adagium: commit now, debug later  :-* :o
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018