Recent

Author Topic: Can Absolute be used on string / packed record  (Read 6419 times)

440bx

  • Hero Member
  • *****
  • Posts: 4028
Re: Can Absolute be used on string / packed record
« Reply #15 on: June 29, 2022, 02:41:01 pm »
The reason for this is that absolute circumvents any typechecking
but, that's the beauty of "absolute", it's the ultimate typecast.  It allows the programmer to typecast any variable to any desired type without any complaints from the compiler.  Of course, that means the programmer should know what he/she is doing.

"absolute" is absolutely great :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Can Absolute be used on string / packed record
« Reply #16 on: June 29, 2022, 02:49:06 pm »
but, that's the beauty of "absolute", it's the ultimate typecast.  It allows the programmer to typecast any variable to any desired type without any complaints from the compiler.  Of course, that means the programmer should know what he/she is doing.

That's only a thing of beauty if it can be packaged in such a way that only high-level wizards can use it. Again, my discussion of a couple of weeks ago relating to moving stuff out of unit files so that it can be locked down by the project manager.

Same applies to pointer arithmetic, and for that matter to pointers themselves in modern practice.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

440bx

  • Hero Member
  • *****
  • Posts: 4028
Re: Can Absolute be used on string / packed record
« Reply #17 on: June 29, 2022, 02:54:12 pm »
Same applies to pointer arithmetic, and for that matter to pointers themselves in modern practice.
"absolute"... pointers.... pointer arithmetic...  this is the stuff programming porn is made of.  :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Can Absolute be used on string / packed record
« Reply #18 on: June 29, 2022, 03:00:54 pm »
The reason for this is that absolute circumvents any typechecking
but, that's the beauty of "absolute", it's the ultimate typecast.  It allows the programmer to typecast any variable to any desired type without any complaints from the compiler.  Of course, that means the programmer should know what he/she is doing.

"absolute" is absolutely great :)
Actually, it is a lack of typecast.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

440bx

  • Hero Member
  • *****
  • Posts: 4028
Re: Can Absolute be used on string / packed record
« Reply #19 on: June 29, 2022, 03:14:23 pm »
Actually, it is a lack of typecast.
No.  It's a redefinition of a variable with a new name and new type.  An address identified with more than one name and type.  The ultimate typecast.  Same address, different name and different type... without any complaints from the compiler.  Great stuff :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Can Absolute be used on string / packed record
« Reply #20 on: June 29, 2022, 03:19:19 pm »
"absolute"... pointers.... pointer arithmetic...  this is the stuff programming porn is made of.  :)

/SPLUTTER/...

You have very strange tastes. I think most people here would prefer APL... used sparingly, strictly within its traditional problem domain, and with adequate comments indicating why it's appropriate.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #21 on: June 29, 2022, 03:21:29 pm »
but, that's the beauty of "absolute", it's the ultimate typecast.  It allows the programmer to typecast any variable to any desired type without any complaints from the compiler.  Of course, that means the programmer should know what he/she is doing.

"absolute" is absolutely great :)
The thing is, I love hacking around with memory layout, e.g. I really like the quake fast inverse square root algorithm, it's a real beauty. The thing is if you do anything special (like memory magic), your code should reflect this special treatment. In the fast inverse square root algorithm it does this via pointer casts:
Code: C  [Select][+][-]
  1. int i = *(int*)&x
So this explicetly states: give me the value of x if I consider the memory as an integer layout. And through the unusual (and cumbersome) syntax it also says on a meta level to any developer reading this: "I am doing something unusual here, and I probably thought about this otherwise why would I do the hassle".

Casts are beautiful, because they allow doing the naughty things, while signaling that what you are doing is special. You can go even further, e.g. C++ has (I think) 5 different cast types, depending if you are casting values (statically or dynamically), pointer or even bitcasts if you want a different type with correct bit representation. All of them doing different type checks so you can explicetly state what you want to do and what the compiler should check.
Personally I think this is a bit overboard (as I often see C++ developers just use the simple C cast which does anything), but at least some indication and checks should remain

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Can Absolute be used on string / packed record
« Reply #22 on: June 29, 2022, 03:34:15 pm »
No.  It's a redefinition of a variable with a new name and new type.  An address identified with more than one name and type.  The ultimate typecast.  Same address, different name and different type... without any complaints from the compiler.  Great stuff :)

Ah. Just like duplicated FORTRAN COMMON blocks, which were widely deprecated (albeit never removed) once modules and decent type checking were added to the language in the 1980s.

Speaking as somebody who can (but would rather not) debug with a logic analyser: things like that have their uses, but not where they can be used by the inexperienced who are unaware that there are safer mechanisms.

It's unfortunate that "absolute" appears so early in a list of reserved words, or a reference manual's index...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Can Absolute be used on string / packed record
« Reply #23 on: June 29, 2022, 03:58:23 pm »
Actually, it is a lack of typecast.
No.  It's a redefinition of a variable with a new name and new type.  An address identified with more than one name and type.  The ultimate typecast.  Same address, different name and different type... without any complaints from the compiler.  Great stuff :)

Yes. Typecasting implies actions might be taken to convert the internal representation of one type to the other - sign extend, zero padding, truncating, rounding, whatever. Putting a variable at the address of another means just another interpretation of the same binary data. Type replacement.

Absolute was the way to access video memory or unprotected system memory in the DOS days. Those are long gone.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Can Absolute be used on string / packed record
« Reply #24 on: June 29, 2022, 04:03:35 pm »
Yes. Typecasting implies actions might be taken to convert the internal representation of one type to the other - sign extend, zero padding, truncating, rounding, whatever. Putting a variable at the address of another means just another interpretation of the same binary data. Type replacement.

In fairness, that's language-dependant and the rules are not always well-defined.

However where one does need bit-by-bit equivalence, Pascal (and Modula-2 etc.) has the untagged variant record.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Can Absolute be used on string / packed record
« Reply #25 on: June 29, 2022, 04:46:05 pm »
Yes. Typecasting implies actions might be taken to convert the internal representation of one type to the other ... truncating ...
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   vCurr  : Currency;
  5.   vInt64 : int64 absolute vCurr;
  6. begin
  7.   { Trunc currency cheaply (much simpler than FPC implementation) }
  8.   vCurr := 12345.6789;
  9.   WriteLn(vCurr);
  10.   vInt64 := vInt64 - (vInt64 mod 10000); // Trunc the currency
  11.   WriteLn(vCurr);
  12.   ReadLn;
  13. end.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Can Absolute be used on string / packed record
« Reply #26 on: June 29, 2022, 04:55:04 pm »
And why not use pointers:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   vCurr  : Currency;
  5.   vInt64 : PInt64 = @vCurr;
  6. begin
  7.   { Trunc currency cheaply (much simpler than FPC implementation) }
  8.   vCurr := 12345.6789;
  9.   WriteLn(vCurr);
  10.   vInt64^ := vInt64^ - (vInt64^ mod 10000); // Trunc the currency
  11.   WriteLn(vCurr);
  12.   ReadLn;
  13. end.    
Typechecking for pointers is still not that strong (compared to other languages like C++ which has a much stronger typchecking), but it's better than absolute. Also by explicetly having to dereference the pointer you add an additional hint that you are modifying some other value, as in long functions this information could be off the screen for someone reading the code

Or even better, use variant records, these have strong typechecking, a centralized point of type connection (so when changing one you are immediately facing with all possible conversions) and a direct binding through a shared identifier, improving readability and error resiliance:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. var
  4.   data: record
  5.     case Boolean of
  6.     True: (vCurr: Currency);
  7.     False: (vInt64: Int64);
  8.   end;
  9. begin
  10.   { Trunc currency cheaply (much simpler than FPC implementation) }
  11.   data.vCurr := 12345.6789;
  12.   WriteLn(data.vCurr);
  13.   data.vInt64 := data.vInt64 - (data.vInt64 mod 10000); // Trunc the currency
  14.   WriteLn(data.vCurr);
  15.   ReadLn;
  16. end.  
« Last Edit: June 29, 2022, 04:58:10 pm by Warfley »

440bx

  • Hero Member
  • *****
  • Posts: 4028
Re: Can Absolute be used on string / packed record
« Reply #27 on: June 29, 2022, 04:57:30 pm »
Absolute was the way to access video memory or unprotected system memory in the DOS days. Those are long gone.
"absolute" is likely more useful these days than it was back then. 

These days, some programs have to deal with a mix of 32bit structures and 64bit structures.  An example of that, a pointer to the optional header of a loaded module. The clean and elegant way of dealing with that situation is to declare the pointer like this:

Code: Pascal  [Select][+][-]
  1. var
  2.   OptionalHeader          : PIMAGE_OPTIONAL_HEADER   = nil;
  3.   OptionalHeader32        : PIMAGE_OPTIONAL_HEADER32 absolute OptionalHeader;
  4.   OptionalHeader64        : PIMAGE_OPTIONAL_HEADER64 absolute OptionalHeader;
  5.  
the PIMAGE_OPTIONAL_HEADER is an empty record while PIMAGE_OPTIONAL_HEADER32 and PIMAGE_OPTIONAL_HEADER64 are fully defined.  This way there is only one pointer (cannot get pointers mixed) and the interpretation of the pointer is determined by the bitness of the module. 

This is good stuff :)

"absolute" is also great stuff when using routines like qsort that are prototyped as receiving generic pointers.  "absolute" allows to overlay the real pointer types on top of the generic pointer types.  bsearch is like that too.  Parsing the exceptions directory or POGO debug information without "absolute" would be a real pain.  When parsing any data structure that consists of variably sized fields of different types, "absolute" and pointer arithmetic save the day. "absolute" is absolutely great... don't code without it! but, just like everything else, it's wonderful if you know how to use it well, otherwise, it could probably be a source of headaches.


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

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Can Absolute be used on string / packed record
« Reply #28 on: June 29, 2022, 05:08:04 pm »
And why not use pointers:
Why make it complicated ?

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Can Absolute be used on string / packed record
« Reply #29 on: June 29, 2022, 05:13:03 pm »
@BrunoK
Thanks for the example, but it wasn't my point, my point was it is not a typecast but type replacement.

@440bx
Haven't you heard about unions (aka variant records)? Both Mark an Warfley noted them.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018