Recent

Author Topic: Have anyone know how to translate C 0-length array to Pascal array?  (Read 10250 times)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11531
  • FPC developer.
Re: Have anyone know how to translate C 0-length array to Pascal array?
« Reply #30 on: April 13, 2024, 11:52:57 am »
Afaik that length is marked using pragma's in C and not necessarily the first. (quite often later versions of the record increased the distance between fields and array)

TYDQ

  • New Member
  • *
  • Posts: 48
Re: Have anyone know how to translate C 0‑length array to Pascal array?
« Reply #31 on: April 14, 2024, 02:42:37 pm »
If I understand correctly, this is a length‑prefixed array. A length‑prefixed array can be achieved with schema data types. Schemata are defined by ISO standard 10206 “Extended Pascal”. Unfortunately the FPC does not support EP, in particular schemata, yet.
Code: Pascal  [Select][+][-]
  1. type
  2.         integerList(length: integer) = array[1..length] of integer;
  3. var
  4.         myList: integerList(5); { = array[1..5] of integer }
  5. begin
  6.         writeLn(myList.length); { writes `5` }
You need to use pointers if the length changes at run‑time; Extended Pascal also permits variable sub‑range boundaries, e. g. the following is legal:
Code: Pascal  [Select][+][-]
  1. program variableSubRangeDemo;
  2.         procedure p(protected length: integer);
  3.                 type
  4.                         arrayIndex = 1..length;
  5.                 var
  6.                         myArray: array[arrayIndex] of integer;
  7.                         i: arrayIndex;
  8.                 begin
  9.                         { Now you have an array[1..5] of integer
  10.                           without using pointers or schema data types. }
  11.                 end;
  12.         begin
  13.                 p(5);
  14.         end.
Again, it is not necessary to invent novel syntax, this feature is not missing in Pascal, it merely lacks FPC support.
So how to make the record adapt to record members which is decided in runtime not in compile time?Does the pointer can decide the array size in runtime and dynamicly decided by the items input?

jamie

  • Hero Member
  • *****
  • Posts: 6234
Re: Have anyone know how to translate C 0-length array to Pascal array?
« Reply #32 on: April 14, 2024, 03:20:42 pm »
@TYDQ

 The array you presented in your attempted translation has no size; you cannot emulate it using standard methods in pascal.

 In C, you can have zero size arrays defined but all this does is allows you to index the memory that is at that immediate location,
 basically, overlapping memory that is there without actually allocating space in the RECORD/STRUCT

  This is commonly done to allow header information which may contain size info for example of following data in memory.

 Have you looked at the solution I provided a few posts back using a Generic that can be expressed with virtually any data type?
 
The only true wisdom is knowing you know nothing

TYDQ

  • New Member
  • *
  • Posts: 48
Re: Have anyone know how to translate C 0-length array to Pascal array?
« Reply #33 on: April 14, 2024, 03:55:16 pm »
@TYDQ

 The array you presented in your attempted translation has no size; you cannot emulate it using standard methods in pascal.

 In C, you can have zero size arrays defined but all this does is allows you to index the memory that is at that immediate location,
 basically, overlapping memory that is there without actually allocating space in the RECORD/STRUCT

  This is commonly done to allow header information which may contain size info for example of following data in memory.

 Have you looked at the solution I provided a few posts back using a Generic that can be expressed with virtually any data type?
So how to modify your code posted in fpc not in objfpc(I don't know how to write an objpas unit)?

Kays

  • Hero Member
  • *****
  • Posts: 586
  • Whasup!?
    • KaiBurghardt.de
Re: Does anyone know how to translate C 0‑length array to Pascal array?
« Reply #34 on: April 14, 2024, 04:55:27 pm »
[…] So how to make the record adapt to record members which is decided in runtime not in compile time?Does the pointer can decide the array size in runtime and dynamicly decided by the items input?
Extended Pascal extends the new procedure to take additional (variable) arguments.
Code: Pascal  [Select][+][-]
  1. program runTimeDiscriminationDemo(input);
  2.         type
  3.                 natural = 1..maxInt;
  4.                 integerList(length: natural) = array[1..length] of integer;
  5.         var
  6.                 requestedLength: natural;
  7.                 integerListLocation: ^integerList;
  8.         begin
  9.                 readLn(requestedLength);
  10.                 new(integerListLocation, requestedLength);
  11.         end.
Again, this works fine with the GPC but does not work with the FPC as of yet.
GPC is abandonware, so don’t bother trying it. I merely use it to verify claims about Extended Pascal.
Yours Sincerely
Kai Burghardt

jamie

  • Hero Member
  • *****
  • Posts: 6234
Re: Have anyone know how to translate C 0-length array to Pascal array?
« Reply #35 on: April 14, 2024, 09:03:37 pm »
Quote
So how to modify your code posted in fpc not in objfpc(I don't know how to write an objpas unit)?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode fpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11. Type
  12. multiboot_color = record
  13.   red, green, blue: byte;
  14. end;
  15.  
  16. multiboot_tag_framebuffer_common = record
  17.   // fields of multiboot_tag_framebuffer_common
  18. end;
  19. TEmptyBootColorArray = Object
  20.    Type
  21.       PT = ^multiboot_Color;
  22.   Private
  23.   Function  fMultiBoot_color(I:Integer):multiboot_color;
  24.   Procedure SetMultiBoot_Color(I:integer; Value:multiboot_color);
  25.   Public
  26.   Property Items[x:integer]:multiboot_Color Read fMultiBoot_color write SetMultiBoot_Color; default;
  27. end;
  28. multiboot_tag_framebuffer = record
  29.   common: multiboot_tag_framebuffer_common;
  30.   case integer of
  31.     0: (
  32.       framebuffer_palette_num_colors: word;
  33.       //framebuffer_palette: array of multiboot_color; //Old translation
  34.       framebuffer_palette:TEmptyBootColorArray;  //New translation
  35.     );
  36.     1: (
  37.       framebuffer_red_field_position: byte;
  38.       framebuffer_red_mask_size: byte;
  39.       framebuffer_green_field_position: byte;
  40.       framebuffer_green_mask_size: byte;
  41.       framebuffer_blue_field_position: byte;
  42.       framebuffer_blue_mask_size: byte;
  43.     );
  44. end;
  45. Function TEmptyBootColorArray.FMultiBoot_color(I:Integer):Multiboot_Color;
  46.  Begin
  47.    fMultiBoot_Color := PT(@Self)[I];
  48.  end;
  49. Procedure TEmptyBootColorArray.SetMultiBoot_Color(I:integer; Value:multiboot_color);
  50. Begin
  51.   PT(@Self)[I] := Value;
  52. end;
  53.  
  54. Var
  55.   T:multiboot_Tag_FrameBuffer;
  56.   B:multiboot_color;
  57. begin
  58.   B.red := 123;
  59.   T.framebuffer_palette[0]:= B; //Example how to set a cell;
  60.   WriteLn('Sizde of Record :',SizeOf(T));
  61.   WriteLn('Value that was entered :',T.FrameBUffer_palette[0].red);
  62.   ReadLn;
  63. end.
  64.  
  65.  

FPC mode, with basic example..
The only true wisdom is knowing you know nothing

TYDQ

  • New Member
  • *
  • Posts: 48
Re: Have anyone know how to translate C 0-length array to Pascal array?
« Reply #36 on: April 15, 2024, 08:07:36 am »
Quote
So how to modify your code posted in fpc not in objfpc(I don't know how to write an objpas unit)?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode fpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11. Type
  12. multiboot_color = record
  13.   red, green, blue: byte;
  14. end;
  15.  
  16. multiboot_tag_framebuffer_common = record
  17.   // fields of multiboot_tag_framebuffer_common
  18. end;
  19. TEmptyBootColorArray = Object
  20.    Type
  21.       PT = ^multiboot_Color;
  22.   Private
  23.   Function  fMultiBoot_color(I:Integer):multiboot_color;
  24.   Procedure SetMultiBoot_Color(I:integer; Value:multiboot_color);
  25.   Public
  26.   Property Items[x:integer]:multiboot_Color Read fMultiBoot_color write SetMultiBoot_Color; default;
  27. end;
  28. multiboot_tag_framebuffer = record
  29.   common: multiboot_tag_framebuffer_common;
  30.   case integer of
  31.     0: (
  32.       framebuffer_palette_num_colors: word;
  33.       //framebuffer_palette: array of multiboot_color; //Old translation
  34.       framebuffer_palette:TEmptyBootColorArray;  //New translation
  35.     );
  36.     1: (
  37.       framebuffer_red_field_position: byte;
  38.       framebuffer_red_mask_size: byte;
  39.       framebuffer_green_field_position: byte;
  40.       framebuffer_green_mask_size: byte;
  41.       framebuffer_blue_field_position: byte;
  42.       framebuffer_blue_mask_size: byte;
  43.     );
  44. end;
  45. Function TEmptyBootColorArray.FMultiBoot_color(I:Integer):Multiboot_Color;
  46.  Begin
  47.    fMultiBoot_Color := PT(@Self)[I];
  48.  end;
  49. Procedure TEmptyBootColorArray.SetMultiBoot_Color(I:integer; Value:multiboot_color);
  50. Begin
  51.   PT(@Self)[I] := Value;
  52. end;
  53.  
  54. Var
  55.   T:multiboot_Tag_FrameBuffer;
  56.   B:multiboot_color;
  57. begin
  58.   B.red := 123;
  59.   T.framebuffer_palette[0]:= B; //Example how to set a cell;
  60.   WriteLn('Sizde of Record :',SizeOf(T));
  61.   WriteLn('Value that was entered :',T.FrameBUffer_palette[0].red);
  62.   ReadLn;
  63. end.
  64.  
  65.  

FPC mode, with basic example..
Thank you!

 

TinyPortal © 2005-2018