Overlaying variables lazarus / 21-General ---------------------------------------------------------------------- mtanner · 2021-02-18 11:12 I am using a record structire that containns an array, call it A:array[1..3] of integer; Some parts of my program use all the values in the arry. Other parts use only a single value. Now it's easy enough to just code a[1], but it would improve code readability, and possibly help to avoid bugs, if I could define something like B:integer "equivalent to" A[1] I should perhap emphasise that I obviously could just code B as a separate field in the record, but I may need to contain memory requirements agressively, and this record may be repeated millions of times. Does it make any difference to the above whther the fields are in a record or object/clas. Related question. I could use either records or objects for storing the data. What is the overhead for a minimal class (just simple constructor/destructor methods) compared to a record? ---------------------------------------------------------------------- Mr.Madguy · 2021-02-18 11:27 There is analog of C++ union. It's syntax is little bit weird, but it works. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TMyAandB = record  case Boolean of    False:(A:array[1..3] of Integer);    True:(B:Integer);end; ---------------------------------------------------------------------- MarkMLl · 2021-02-18 11:36 Quote from: Mr.Madguy on February 18, 2021, 12:27:37 pmThere is analog of C++ union. It's syntax is little bit weird, but it works. Often referred to as an "untagged variant record", as distinct from Code: [Select]TMyAandB = record   case fullArray: Boolean of     False:(A:array[1..3] of Integer);     True:(B:Integer); end; which has an explicit tag which can be checked by code at runtime. However TBH I'd stick to the classic A[1] notation and investigate different levels of optimisation etc. The reason is that if at some point you replaced your fixed-size array with a dynamically-sized one you'd be likely to find that your B overlaid the data structure providing access to the array storage which would be on the heap, rather than to A[1] itself. That change would also break attempts to cache a pointer to A[1] etc. I'm not saying that you /would/ make that change, just that it's something to bear in mind and in the general case is an argument against trying to be clever. MarkMLl ---------------------------------------------------------------------- Kays · 2021-02-18 12:02 Quote from: mtanner on February 18, 2021, 12:12:57 pm[…] B:integer "equivalent to" A[1] […] Inside a routine you can write: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  b: int64 absolute parameter.value[1];This will not reserve any additional space, but just “alias” the memory location specified after absolute. It can, of course, become repetitive if you have to type that out in every routine, so let’s make it part of your data structure: Properties typically redirect read/write access, but this requires an object or at least {$modeSwitch advancedRecords+}:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  triple = array[1..3] of int64;  data = object      value: triple;    property firstValue: int64 read value[1] write value[1];    end; Quote from: mtanner on February 18, 2021, 12:12:57 pmDoes it make any difference to the above whther the fields are in a record or object/clas.As soon as an object type contains virtual methods it differs from a record. The nice thing about objects is inheritance. Quote from: mtanner on February 18, 2021, 12:12:57 pm[…] What is the overhead for a minimal class (just simple constructor/destructor methods) compared to a record?In short: If you do not use polymorphism or other OOP features, the overhead doesn’t pay off. I say KISS. ---------------------------------------------------------------------- speter · 2021-02-18 22:56 Another way of "skinning the cat" would be to use a pointer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  triple = array [1..3] of integer;var  foo : triple = (6,5,4);  p : ^integer;begin  p := @foo[1];  writeln(p^);end; If using "mode delphi" you could maybe leave off the ^ in the writeln(p^). cheers S. ---------------------------------------------------------------------- avra · 2021-02-19 08:54 Yet another way would be to use https://wiki.freepascal.org/Absolute. I would prefer variant record (union) as the safest of all shown methods. ---------------------------------------------------------------------- CCRDude · 2021-02-19 10:18 Depending on the complexity, §MODESWITCH ADVANCEDRECORDS and Getter/Setter record functions might do some good. https://wiki.freepascal.org/Record#Advanced_record Or simply record helpers. Or if you've got an array, defined as your own type, a type helper. https://wiki.freepascal.org/Helper_types