Definition C array to Pascal array lazarus / 62-General ---------------------------------------------------------------------- fpctuxe · 2021-05-30 15:54 Hello, I’m porting a C library to pascal. In the c- header file I found a array definition. I have problems to translate to pascal. The structures are used to allocate a buffer and returned with  pdesc_list. Example 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 struct desc {  unsigned int length;  unsigned int count;}; struct desc_list {  unsigned int num_desc;  struct desc descs[];          //here are the problem.};desc_list *pdesc_list; Pascal: 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 desc = record  length: Cardinal;  count: Cardinal; end; desc_list = record  num_desc: Cardinal  descs: array [] of desc;      //is not pascal conform.end;pdesc_list =^desc_list; descs: array of desc;   //Is a Pointer to a array (dynamic array). descs: array [0..1000000] of desc; //This works, but is not ideal. My question: Gives a pascal declaration equivalent to C. Thanks for your help. [Edited to add code tags - please see How to use the Forum] ---------------------------------------------------------------------- skalogryz · 2021-05-30 16:22 if this is a GCC code with Zero-length array extension, then you might want to try this: 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 desc_list = record  num_desc: Cardinal  descs: array [0..0] of desc;     // remember to disable Range check when using desc_listend;You can still access the array as following 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   l : desc_list;  i : integer;begin  i:=100;  l.descs[i].count  but it would cause an error if range checks are on ---------------------------------------------------------------------- fpctuxe · 2021-05-30 17:30 Thanks, this is it. 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  l : desc_list;  i : integer;begin  i:=100;{$PUSH}{$WARN 4110 OFF}    l.descs[i].count:= 12345{$POP} Is this the right way to disable range check? ---------------------------------------------------------------------- fpctuxe · 2021-05-30 18:10 This is not the right way. ---------------------------------------------------------------------- fpctuxe · 2021-05-30 18:14 This is it. 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  l : desc_list;  i : integer;begin  i:=100;{$PUSH}{$WARN 4110 OFF} {$RANGECHECKS OFF}    l.descs[i].count:= 12345{$POP} or not? ---------------------------------------------------------------------- 440bx · 2021-05-30 18:17 Quote from: fpctuxe on May 30, 2021, 08:14:25 pmor not? Another option is to simply _not_ enabling range checks at all.  That way, there is no need to disable them. ---------------------------------------------------------------------- jamie · 2021-05-30 22:00 That  is an empty Array at the end of the tunnel.. Pascal does not have that and it can cause problems because the Record now becomes larger than it should if you use a [0..0] which gives you 1 element. The C implementation does not give you any elements, it uses the first slot after the record/Struct. Until the DEV decide to advance the compiler a little you will need to live with hacks. Use a Blank Record at the end , give it a name and then type cast over it when using it. Record  Some fields; EmptyArray :Record End; End; To use it. either use the absolute key word to point an array to EmptyArray or type cast on the fly. ---------------------------------------------------------------------- jamie · 2021-05-30 23:15 here is a concept model of using a helper on the record to make life easier.. 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 unit Unit1; {$mode objfpc}{$H+}{$modeSwitch AdvancedRecords} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type TDesc = Record    Length,Count:Cardinal; end; PDesc = ^TDesc; TDesc_List = Record    num_desc:Cardinal;    Descs:Record End; end;TDisc_ListHelper = Record Helper for TDesc_List Procedure SetDesc(index:Integer;aDesc:TDesc);inline; Function GetDesc(Index:Integer):TDesc;inline;end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm}Procedure TDisc_ListHelper.SetDesc(index:Integer;ADesc:TDesc);begin  PDesc(@Descs)[Index]:= ADesc;end;Function TDisc_ListHelper.GetDesc(Index:Integer):TDesc;Begin Result := PDesc(@Descs)[Index];End;{ TForm1 } procedure TForm1.Button1Click(Sender: TObject);{A dummy test }var  A:TDesc_List;begin  A.num_desc :=-1;  Caption := A.GetDesc(-1).Count.Tostring;end; end.  all of this wouldn't be needed if an empty array could be applied at the end.. ---------------------------------------------------------------------- skalogryz · 2021-05-31 02:41 Quote from: fpctuxe on May 30, 2021, 08:14:25 pmThis is it. .. or not? yes using {$RANGECHECKS OFF} is the right way Quote from: jamie on May 31, 2021, 01:15:19 am...all of this wouldn't be needed if an empty array could be applied at the end..  the feature is not even a part of the official C-standard. why would it be needed for the implementation in pascal? One could do an implementation like this: 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 desc = record  length : Cardinal;  count  : Cardinal;end; desc_list = record  num_desc : Cardinal;  descs    : array [0..0] of desc;     // remember to disable Range check when using desc_listend; tdescarray = array [word] of desc; // even "Integer" would work for 64-bit modepdescarray = ^tdescarray; desc_listPtr = record  num_desc : Cardinal;  descs    : pdescarray;end; procedure InitDescList(const inp: desc_list; out pt: desc_listPtr )begin  pt.numdesc := inp.numdesc;  pt.descs := @inp.descs;end; and dealing with "pt.desc" would allow to avoid RANGECHECKS off (assuming num_desc is less than 65535... which usually it's less than that) Another approach is similar to suggested originally: 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 desc_list = record  num_desc : Cardinal;  descs    : array [word] of desc;     // array[byte]?end;  The actually approach on how to deal with the C-specific type should be chosen based on several things: * is the type only returned from the library (and doesn't need be built by the program) * what the maximum num_desc is "logically" expected/possible. (could a type declared with a certain amount of entries in the array) * is the data returned from desc_list will be copied over to some other storage within the program. (if yes, then dealing with RANGECHECKS off one time, will solve the problem) ---------------------------------------------------------------------- jamie · 2021-05-31 10:43 I see this is going to turn into a long discussion of antics so I will keep it short as this will most likely be the last post I place here. C language allows empty Arrays at the end of a struct or actually in the middle too depending on the code structure. This tactic has been used for years and its even in the C++ world. The idea Is to place a TAG (Address) marker of an array that would appear after the struct so that TAG, that being the name of the empty array,  actually points outside of the struct just following its declaration.  The closes fpc has to offer is an Empty Record at the end for which its TAG will be pointing to the start of any following memory.  if the coder does not mind using a type cast on the fly then a type can be used to enclose this TAG..  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    TMyCast = Array[0..0] of TDesc;   Then in theory 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 TMyCast(MyRecordInstance.TheRecordTag)[?] :=?   Using a variable for the index at runtime should avoid compiler stoppage.. Have a good day and may the bugs be many. ---------------------------------------------------------------------- Kays · 2021-05-31 20:30 Quote from: 440bx on May 30, 2021, 08:17:36 pmQuote from: fpctuxe on May 30, 2021, 08:14:25 pmor not? Another option is to simply _not_ enabling range checks at all.  That way, there is no need to disable them. Yes, but it is in fact good practice to surround the specific LOC with a {$push}{$R-} and {$pop} if you are relying on that behavior. During development it’s a good idea to compile and use a program compiled with ‑dDEBUG [cf. /etc/fpc.cfg], and even if you don’t the {$rangeChecks off} still serves documentation purposes. ---------------------------------------------------------------------- MarkMLl · 2021-05-31 20:50 Quote from: jamie on May 31, 2021, 12:43:18 pmC language allows empty Arrays at the end of a struct or actually in the middle too depending on the code structure. ... The idea Is to place a TAG (Address) marker of an array that would appear after the struct so that TAG, that being the name of the empty array,  actually points outside of the struct just following its declaration. Thanks for that Jamie, succinct and useful. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-05-31 20:53 Quote from: Kays on May 31, 2021, 10:30:13 pmQuote from: 440bx on May 30, 2021, 08:17:36 pmAnother option is to simply _not_ enabling range checks at all.  That way, there is no need to disable them. Yes, but it is in fact good practice to surround the specific LOC with a {$push}{$R-} and {$pop} if you are relying on that behavior. During development it’s a good idea to compile and use a program compiled with ‑dDEBUG [cf. /etc/fpc.cfg], and even if you don’t the {$rangeChecks off} still serves documentation purposes. I agree. Unless a body of code has been /proven/- and I'm using the word in the strict mathematical sense with everything that implies- to be incapable of an out-of-range operation, I have a lot of difficulty envisaging a situation where simply accepting an erroneous result can be considered even remotely acceptable. MarkMLl ---------------------------------------------------------------------- 440bx · 2021-05-31 22:18 Quote from: Kays on May 31, 2021, 10:30:13 pmQuote from: 440bx on May 30, 2021, 08:17:36 pmQuote from: fpctuxe on May 30, 2021, 08:14:25 pmor not? Another option is to simply _not_ enabling range checks at all.  That way, there is no need to disable them. Yes, but it is in fact good practice to surround the specific LOC with a {$push}{$R-} and {$pop} if you are relying on that behavior. During development it’s a good idea to compile and use a program compiled with ‑dDEBUG [cf. /etc/fpc.cfg], and even if you don’t the {$rangeChecks off} still serves documentation purposes. I can agree that having {$rangeChecks off} serves documentation purposes.  That said, a program should _not_ depend on range checking to operate correctly.  The other problem with enabling range checks is that, the final product will usually not be shipped with range checks enabled, which in turn means that the programmer is not gaining experience with code that will be part of the final product.  It's important and very useful to keep the software being developed as close as possible to the version that will be shipped.  This avoids "bad surprises" after the software is shipped. range checks should only be enabled when a problem is found during development and range checking may contribute to determining the cause of the problem.  The rest of the time, it should be disabled (as it will be in the final product.) ---------------------------------------------------------------------- PascalDragon · 2021-06-01 07:04 Quote from: 440bx on June 01, 2021, 12:18:11 amrange checks should only be enabled when a problem is found during development and range checking may contribute to determining the cause of the problem.  The rest of the time, it should be disabled (as it will be in the final product.) The point was about code that only works correctly with range checking disabled. This should be guarded correctly like mentioned above so that if one decides to enable range checking (either because one develops with them enabled or because one has a problem that one hopes to find with range checks) then one doesn't have to hunt false positives. ---------------------------------------------------------------------- MarkMLl · 2021-06-01 08:32 Quote from: 440bx on June 01, 2021, 12:18:11 amThat said, a program should _not_ depend on range checking to operate correctly. I don't believe that anybody suggested that. The argument is over whether simply ignoring (the possibility of) range check errors is ever acceptable. MarkMLl ---------------------------------------------------------------------- 440bx · 2021-06-01 08:44 The point I was trying to make, which apparently I didn't make well, is that range checking should only be used as a debugging aid and only when it may be helpful and not normally be enabled during development.  That eliminates the need for directives to turn it off (because it's normally off.) ---------------------------------------------------------------------- MarkMLl · 2021-06-01 09:08 Quote from: 440bx on June 01, 2021, 10:44:15 amThe point I was trying to make, which apparently I didn't make well, is that range checking should only be used as a debugging aid and only when it may be helpful and not normally be enabled during development.  That eliminates the need for directives to turn it off (because it's normally off.) Range checking should only be disabled if a program can be formally proven to never need it. MarkMLl ---------------------------------------------------------------------- 440bx · 2021-06-01 09:21 Quote from: MarkMLl on June 01, 2021, 11:08:22 amRange checking should only be disabled if a program can be formally proven to never need it. The way I think about it is, if a program is bug-free (which is what it should be) then it doesn't need range checking or any other mechanisms to trap bugs. Except is some rare cases, it is usually not possible to formally prove a program is bug free but, I don't believe that should be used as a reason to enable bug-trapping mechanisms. ---------------------------------------------------------------------- PascalDragon · 2021-06-01 11:17 Quote from: 440bx on June 01, 2021, 10:44:15 amThe point I was trying to make, which apparently I didn't make well, is that range checking should only be used as a debugging aid and only when it may be helpful and not normally be enabled during development.  That eliminates the need for directives to turn it off (because it's normally off.) But the point is if you enable range checking then you want to deal with the problem you want to tackle and not with potential false positives. Thus explicitly disabling of range checks for selected parts where one knows that a range check would trigger, but where it is a false positive is part of "defensive programming" in my opinion. Also I don't agree that these checks should be off during development. They should be off for the final release, yes, but during development they can catch errors that would otherwise manifest as bugs (for the compiler itself these checks have already revealed quite a bunch during development before even comitting). ---------------------------------------------------------------------- MarkMLl · 2021-06-01 11:29 Quote from: PascalDragon on June 01, 2021, 01:17:16 pmAlso I don't agree that these checks should be off during development. They should be off for the final release, yes, but during development they can catch errors that would otherwise manifest as bugs (for the compiler itself these checks have already revealed quite a bunch during development before even comitting). Quite frankly I'm not convinced that they should be off for release code. Now that CPUs no longer provide fine-grained protection (e.g. using segments) the more help that the system gets to detect random errors caused by stray radiation or thermal effects the better. MarkMLl ---------------------------------------------------------------------- jamie · 2021-06-01 15:14 Quote Quite frankly I'm not convinced that they should be off for release code. Now that CPUs no longer provide fine-grained protection (e.g. using segments) the more help that the system gets to detect random errors caused by stray radiation or thermal effects the better. MarkMLl Why not? You don't believe in "I test my work in production" ? ---------------------------------------------------------------------- MarkMLl · 2021-06-01 16:18 Quote from: jamie on June 01, 2021, 05:14:00 pm Why not? You don't believe in "I test my work in production" ? I don't. But now that comparatively few systems have ECC memory, and the best that hardware protection can offer is fairly coarse-grained process-based memory protection, the more checking of instructions and intermediate results the better. MarkMLl ---------------------------------------------------------------------- 440bx · 2021-06-01 16:58 Quote from: PascalDragon on June 01, 2021, 01:17:16 pmAlso I don't agree that these checks should be off during development. They should be off for the final release, yes, but during development they can catch errors that would otherwise manifest as bugs (for the compiler itself these checks have already revealed quite a bunch during development before even comitting). We have a different philosophy about development.  Myself, I want to see the bugs in what is always close to the final product during development.  If there is a bug then, the aids the compiler provides are welcome (just like having a good debugger is always welcome.)  Also, not having the "artificial protections" shows how the program behaves when a particular bug is present.  That experience is often useful when a customer describes some weird situation that makes the program fail because it's likely the developer saw similar behavior during development. I believe it's important to keep the product as close to the final product during development in order to gain experience with how the program behaves when some bug is triggered.  I want to see every bug imaginable during development, that way I know how the program behaves in the presence of a particular bug. ---------------------------------------------------------------------- ASerge · 2021-06-01 18:07 Quote from: fpctuxe on May 30, 2021, 05:54:25 pmI’m porting a C library to pascal. In the c- header file I found a array definition. I have problems to translate to pascal. The structures are used to allocate a buffer and returned with  pdesc_list. My question: Gives a pascal declaration equivalent to C. In my opinion a couple of years ago it was already discussed and I gave a solution. Example (without range errors): 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 {$MODE OBJFPC}{$MODESWITCH ADVANCEDRECORDS}{$RANGECHECKS ON} // Even so!{$APPTYPE CONSOLE} type  PDesc = ^TDesc;  TDesc = record    Length: Cardinal;    Count: Cardinal;  end;   PDescList = ^TDescList;  TDescList = packed record    NumDesc: Cardinal;    function Descs: PDesc; inline;  strict private    DescsStart: record end;  end; function TDescList.Descs: PDesc;begin  Result := PDesc(@DescsStart);end; procedure Test;const  CCount = 100;var  P: PDescList;  i: Integer;begin  GetMem(P, SizeOf(TDescList) + CCount * SizeOf(TDesc));  try    P^.NumDesc := CCount;    i := CCount - 1;    P^.Descs[i].Count := 12345;    Writeln(P^.Descs[i].Count);  finally  FreeMem(P);  end;end; begin  Test;  Readln;end. ---------------------------------------------------------------------- PascalDragon · 2021-06-02 07:14 Quote from: MarkMLl on June 01, 2021, 01:29:07 pmQuote from: PascalDragon on June 01, 2021, 01:17:16 pmAlso I don't agree that these checks should be off during development. They should be off for the final release, yes, but during development they can catch errors that would otherwise manifest as bugs (for the compiler itself these checks have already revealed quite a bunch during development before even comitting). Quite frankly I'm not convinced that they should be off for release code. Now that CPUs no longer provide fine-grained protection (e.g. using segments) the more help that the system gets to detect random errors caused by stray radiation or thermal effects the better. Pages which usually are 4 KiB are much more fine grained than what segments offered (64 KiB). Also current CPUs provide the option to mark pages as "no execute" and any number of page can be marked as such while only a single segment could be the code segment. Not to mention that one doesn't have the hassle with far calls to switch from one code segment to the other. ---------------------------------------------------------------------- MarkMLl · 2021-06-02 08:08 Quote from: PascalDragon on June 02, 2021, 09:14:20 amPages which usually are 4 KiB are much more fine grained than what segments offered (64 KiB). Also current CPUs provide the option to mark pages as "no execute" and any number of page can be marked as such while only a single segment could be the code segment. Not to mention that one doesn't have the hassle with far calls to switch from one code segment to the other. Disagree. The granularity of x86-style segmentation was 16 bytes, and then you had precise bounds-checking of arrays. However (a) doing this thoroughly rapidly exhausted the descriptor tables and (b) bounds checking is incomplete for things like network messages which might not fill the defined type but should still be protected. BTDT. MarkMLl