Bookstore

Recent

Author Topic: How to Alias a Variable  (Read 1054 times)

woodybrison

  • Newbie
  • Posts: 4
How to Alias a Variable
« on: January 30, 2020, 06:52:03 am »
I want to declare an array of uint32, fill it from a source of uint32's, then pull uint8s out of it, where the address of the uint8 would be just 4 times the address of the uint32 plus an offset.
   There used to be a feature in Turbo Pascal to do this, IIRC it was called aliasing. Anybody remember that? You could declare say a string, then alias it as an array of bytes... stuff like that.
   How to do this in Free Pascal?

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: How to Alias a Variable
« Reply #1 on: January 30, 2020, 06:59:30 am »
You could declare say a string, then alias it as an array of bytes... stuff like that.

I use Absolute to do it.
https://wiki.freepascal.org/Absolute
https://www.delphigeist.com/2009/09/absolute-directive.html

Thaddy

  • Hero Member
  • *****
  • Posts: 9782
Re: How to Alias a Variable
« Reply #2 on: January 30, 2020, 08:32:27 am »
I would also use absolute, but it is also possible to typecast to a fixed size byte array four times the size of the uint32 array (which is in effect the same thing)
I am more like donkey than shrek

howardpc

  • Hero Member
  • *****
  • Posts: 3355
Re: How to Alias a Variable
« Reply #3 on: January 30, 2020, 08:39:40 am »
You could also use an array of variant records.
Code: Pascal  [Select]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$IfDef windows}{$AppType console}{$EndIf}
  5.  
  6. type
  7.  
  8.   RAlias = record
  9.   case Boolean of
  10.     True:  (int32: UInt32);
  11.     False: (int8_1: UInt8; int8_2: UInt8; int8_3: UInt8; int8_4: UInt8);
  12.   end;
  13.  
  14. var
  15.   ra: RAlias;
  16.  
  17. begin
  18.   ra.int32 := $04030201;
  19.   WriteLn('int8_1=',ra.int8_1,' int8_2=',ra.int8_2,' int8_3=',ra.int8_3,' int8_4=',ra.int8_4);
  20.   ReadLn;
  21. end.

woodybrison

  • Newbie
  • Posts: 4
Re: How to Alias a Variable
« Reply #4 on: January 30, 2020, 04:19:26 pm »
Thank you, thank you, may all your children be rich and wise

woodybrison

  • Newbie
  • Posts: 4
Re: How to Alias a Variable
« Reply #5 on: February 04, 2020, 11:38:12 pm »
Again, thank you all. Along the lines of giving as well as receiving, I here present an interesting detail.

Going around a loop, at one point in the loop reading a byte from a file; this is too slow. I would like to read in big blocks from the file, then at the end with less than 1 block left I'd read in the last few bytes as bytes. The idea is that I can read a block of say 128 bytes a lot faster than 128 individual bytes.

I could always just read a lot of big blocks, then close and reopen the file for random byte access to get the last few. But just for fun let's see if I can use "absolute" on file handles.

I will define blocks of ten characters, to see if the idea works in principle where I can use write() to see the results before I proceed with huge blocks and humongous files. See if I can read those in then whether the file is also open for character reading, if the file pointer works right...

Code: Pascal  [Select]
  1. Program experiment;
  2.  
  3. type
  4.   block = array[ 0..9 ] of char;
  5.  
  6. var
  7.   blockFile : file of block;
  8.   charFile : file of char absolute blockFile;
  9.  
  10.   blockArray : array[0..4] of block;
  11.   charArray : array[0..49] of char absolute blockArray;
  12.   i : uint32;
  13.  
  14. begin
  15. for i := 0 to 49 do
  16.   charArray[ i ] := '-';
  17.  
  18. assign( blockFile, 'sample.txt' );
  19. reset( blockFile );
  20. for i := 0 to 2 do
  21.   read( blockFile, blockArray[ i ] );
  22. for i := 30 to 37 do
  23.   read( charFile, charArray[ i ] );
  24.  
  25. close( blockFile );
  26.  
  27. for i := 0 to 49 do
  28.   write( charArray[ i ] );
  29.  
  30. end.
  31.  

input file:
this is a test of the absolute feature of Free Pascal. I don't know if this will work

results
this is a test of the absolute feature------------

It would appear that it really does work. Not sure why but I surmise that the file pointer is always in bytes. When I read a block, the pointer is advanced by <blocksize> bytes.