Variant Record Behavior lazarus / 62-General ---------------------------------------------------------------------- simone · 2024-11-01 11:04 I confess, I have not used variant records very often during the nearly 35 years that I have been programming in (Object) Pascal. For this reason, probably, I had missed a subtle behavior, which I illustrate with the following example. 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 program project1;type  TSide=integer;   TPinCase=(psRectangle, psEllipse, psPolygon);   TBorderPoint=record    case TPinCase of      psRectangle : (Side : TSide);      psEllipse   : (Angle : integer);      psPolygon   : (Segment : integer; Position : integer);  end; var  P : TBorderPoint; begin  P.Side:=1;  P.Angle:=300;  writeln(P.Side); // print 300, not 1  readln;end. Side and Angle are fields belonging to two different cases. If I first assign Side, then Angle, the latter value overwrites Side. For this reason, the example shows 300, not 1. From a technical point of view, I understand the reasons for this behavior. In the variant records the fields of the different cases share the same memory layout. Basically they are like unions in C. Since this it seems to me that this behavior is not documented, I ask you if this is actually the expected behavior or an implementation dependent behavior. Thanks for the usual support. ---------------------------------------------------------------------- 440bx · 2024-11-01 11:21 It is the expected behavior. Pascal variant part in a record is C's union.  They share the memory and that is documented. HTH. ---------------------------------------------------------------------- Thaddy · 2024-11-01 11:21 It IS basically like a union in C. Always has been, although there are subtle differences, it uses indeed the same memory layout. Documentation: https://www.freepascal.org/docs-html/ref/refsu15.html ---------------------------------------------------------------------- 440bx · 2024-11-01 11:30 I was looking for the documentation regarding the memory allocation applicable to the variant part of a record and could not find it. I looked online (Wiki) and in the Language Reference Guide and did not find anywhere an explanation regarding the variant part's memory usage. It's definitely documented in standard Pascal. ---------------------------------------------------------------------- simone · 2024-11-01 11:38 @440bx @Thaddy, Thanks for confirming my interpretation. Variant records exist since the beginning of Pascal, already described in PASCAL - User Manual and Report by Jensen & Wirth (page 44 in the second edition I own). But I have not found, to my memory, the specification of this behavior in the literature. Where can I find it? Edit: I don't seem to me that such a specification is in ISO 7185 and ISO 10206 documents ---------------------------------------------------------------------- 440bx · 2024-11-01 13:01 The original Pascal standard (J&W), 6.2.2, page 148, states: QuoteA  record  type  may have  several  variants,  in  which  case  a  certain field  may  be  designated  as  the  tag field,  whose  value  indicates which  variant  is  assumed  by  the  record  variable  at  a  given  time. That passage implies that only 1 variant at a time can be active.  This in turn implies they share memory. I could not find anything that even implied it in the more recent Pascal standards. ---------------------------------------------------------------------- simone · 2024-11-01 15:45 You are right. It is in the report part of the book you cited, but not in the user manual part that I had seen. However I believe this behavior should be explicitly described in the current official documentation. ---------------------------------------------------------------------- Warfley · 2024-11-01 19:14 Quote from: 440bx on November 01, 2024, 02:01:52 pmThat passage implies that only 1 variant at a time can be active.  This in turn implies they share memory. Not necessarily. Take the following code: 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 generic TMyUnion = record  Selected:Boolean;  first: T;  Second: U;   procedure setSelected(AValue: Boolean);end; procedure TMyUnion.setSelected(AValue: Boolean);begin  if selected=AValue then Exit;  if selected then  begin    Finalize(first);    Initialize(second);  end  else  begin    Finalize(Second);    Initialize(first);  end;  Selected:=AValue;end; Now first and second do not share memory, but only ever one will be initialized (and thereby valid to be accessed) while the otherone will not. Of course such an implementation does not make that much sense, because it's the exact same as if they'd be sharing memory, so basically it's just less efficient, but still it's possible to implement that way.. Also as the comparison to C unions was drawn, I feel the need to point out that the C standard says about unions: QuoteWhen a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values.So while the overlapping memory allowing to read out the value in different bit reperesentation, this is unspecified behavior and implementation dependent. It is not a feature of C itself but rather of GCC/Clang/whatever C compiler you use. (Note that it says "unspecified" and not "undefined", which means it is implementation dependent, while "undefined" would mean it's never correct to read it out) ---------------------------------------------------------------------- 440bx · 2024-11-01 21:33 Quote from: simone on November 01, 2024, 04:45:57 pmHowever I believe this behavior should be explicitly described in the current official documentation. I agree.  The fact that there is only one storage area to hold a single set of independent variables should be made crystal clear.  Also, that is a very important fact that should be explicitly made and easy to find. I know I've seen it clearly documented somewhere but, I don't recall where.  Probably read it in some Pascal book or manual sometime years ago. Maybe the omission or lack of clarity warrants a bug report against the documentation.  Like you, I am inclined to believe it does. ---------------------------------------------------------------------- simone · 2024-11-01 22:15 In the book Oh Pascal by Doug Cooper and Michael Clancy, pages 413-414, there is a clear explanation of memory layout overlaying in variant records. Very nice book. (@440bx I remember you know this book  ) ---------------------------------------------------------------------- 440bx · 2024-11-01 22:31 Yes, I consider "Oh Pascal" one of the best programming books ever written, not only it teaches the language extremely well, it emphasizes good programming methodologies.  Yes, excellent book. I'm glad you enjoy it too. ---------------------------------------------------------------------- Thaddy · 2024-11-04 10:13 Quote from: Warfley on November 01, 2024, 08:14:32 pm(Note that it says "unspecified" and not "undefined", which means it is implementation dependent, while "undefined" would mean it's never correct to read it out) Well it is actually unspecified, especially when local. Stack is dirty, any excess memory is unspecified unless there is a fully filled alternative. ---------------------------------------------------------------------- Warfley · 2024-11-04 11:52 Quote from: Thaddy on November 04, 2024, 11:13:33 amWell it is actually unspecified, especially when local. Stack is dirty, any excess memory is unspecified unless there is a fully filled alternative. Not only that but also the standard is written in a way that makes it completely portable. But as soon as you talk memory layout things like endianess and stuff are very much hardware dependant. Which is why the C standard only allows you to treat memory regions as different types, if they are a strict prefix of one another. So the following is not standard C: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i typedef struct {  uint32_t a;} s1_t;typedef struct {  uint16_t w1, w2;} s2_t; s1_t mystruct;(s2_t *)&mystruct->w1 = 42;But the following is: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i typedef struct {  uint32_t a;} s1_t;typedef struct {  uint32_t a, b;} s2_t; s2_t mystruct;(s1_t *)&mystruct->a = 42;Because here you only "downsize" the record, and C guarantees the order of elements and layout of prefixes. Anything else requires you to check with the behavior your compiler guarantees for the platform you are on.