double pointers lazarus / 62-General ---------------------------------------------------------------------- fcu · 2025-01-31 13:52 Hi working with double pointers in pascal is something that i have never tried before i tried converting this simple code to pascal but it seems difficult some how could some one please show as how this code look like in pascal ? 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 #include  int ** ptr; int main(){// allocate  ptr = new int*[8];  for (int i=0; i<8; i++)    ptr[i] = new int[8];    for (int i=0; i<8; i++)        for (int j=0; j<8; j++)      ptr[j][i] = j;              // print  for (int i=0; i<8; i++){        for (int j=0; j<8; j++){    printf("%d", ptr[j][i]);            }        printf("\n");     }  // free memory  for (int i=0; i<8; i++)        delete  ptr[i];   delete [] ptr;  return 0;} ---------------------------------------------------------------------- d2010 · 2025-01-31 14:12 Quote from: fcu on January 31, 2025, 02:52:27 pmHi working with double pointers in pascal is something that i have never tried before i tried converting this simple code to pascal but it seems difficult some how could some one please show as how this code look like in pascal ? 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 #include  int ** ptr; int main(){// allocate  ptr = new int*[8];  for (int i=0; i<8; i++)    ptr[i] = new int[8];    for (int i=0; i<8; i++)        for (int j=0; j<8; j++)      ptr[j][i] = j;              }  // free memory  for (int i=0; i<8; i++)        delete  ptr[i];   delete [] ptr;  return 0;}   Your source inside C++ is worst,  even with VC++ , but ?? in Delphi/Lazarus. You  do not make 100* getmem or 100* new(allocate-memory). You make one, but only once  getmem and only once freemem. Code: [Select]Program ArsenieBoca_Multumesc; {$apptype consoleapp} Const maxC=255;         maxL=255; Type MatrixPtr=array[0..maxL,0..maxC] of integer;      tap_Matric=^ MatrixPtr; Var ptr0:tap_Matric      i,j:integer;   Begin       ptr0:=nil;       Randomize;       i:=00;       j:=00;        Getmem(ptr0,sizeof(MatrixPtr));        for i:=0 to 100 do          for j:=0 to 100 do              ptr0^[i,j]:=random(1203);         sum:=00;        for i:=0 to 100 do  for j:=0 to 100 do  sum:=sum+ptr^[i,j];        writeln('Al-sum=',sum  );        readln;        freemem(ptr0);        ptr:=nil; End; Always, you reduce numbers-of-call("freemem")  and you reduce numbers-of-call("GetMem")  to minimum as posibile. You can user dynamic  of lines or columns, but count-of only  < 255. ---------------------------------------------------------------------- 440bx · 2025-01-31 15:18 Quote from: fcu on January 31, 2025, 02:52:27 pmHi working with double pointers in pascal is something that i have never tried before i tried converting this simple code to pascal but it seems difficult some how could some one please show as how this code look like in pascal ? 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 #include  int ** ptr; pascal:  type  Ptr2Integer     = ^integer;  Ptr2Ptr2Integer = ^Ptr2Integer; var  ptr : Ptr2Ptr2Integer;  i, j  : integer; int main(){  // pascal: just put the statements between the "begin" and "end."// allocate  ptr = new int*[8];        // pascal: Getmem(ptr, 8 * sizeof(ptr^));  for (int i=0; i<8; i++)   // pascal: for i := 0 to 7 do    ptr[i] = new int[8];    // pascal: Getmem(ptr[i], 8 * sizeof(ptr[i]^));    for (int i=0; i<8; i++)        // pascal: for i := 0 to 7 do         for (int j=0; j<8; j++)  // pascal: for j := 0 to 7 do      ptr[j][i] = j;             // pascal: ptr[i][j] := j;   // note: the indexes in the C code are reversed, that's conceptually incorrect.  // print  for (int i=0; i<8; i++){         // pascal: for i := 0 to 7 do begin        for (int j=0; j<8; j++){     // pascal:   for j := 0 to 7 do begin      printf("%d", ptr[j][i]);       // pascal: write(ptr[i][j];        }                          // pascal: end;        printf("\n");                // pascal: writeln;  }                                  // pascal: end;  // free memory  for (int i=0; i<8; i++)      // pascal: for i := 0 to 7 do        delete  ptr[i];        // pascal: freemem(ptr[i], 8 * sizeof(ptr[i]^));   delete [] ptr;               // pascal: freemem(ptr, 8 * sizeof(ptr^));  return 0;                    // pascal: nothing}  Untested but, it should be pretty close.  Note that the array indexes in the C code are reversed and the only reason the code will work is because the arrays are the same size, if they weren't that code would make a mess.  The Pascal code has the indexes as they should be, therefore the Pascal code is not fully equivalent to the C code. ---------------------------------------------------------------------- fcu · 2025-01-31 19:12 aha , my fault i thought getmem works just like NEW in c++(you don't have to specify the size of the type) now it works thank you ---------------------------------------------------------------------- 440bx · 2025-01-31 19:21 Quote from: fcu on January 31, 2025, 08:12:32 pmthank you You're welcome. ---------------------------------------------------------------------- ASerge · 2025-02-01 00:51 Quote from: fcu on January 31, 2025, 02:52:27 pm...how could some one please show as how this code look like in 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 var  Arr: array of array of Integer;  i, j: Integer;begin  // allocate  SetLength(Arr, 8, 8);   for i := 0 to 7 do    for j := 0 to 7 do      Arr[j][i] := j;   // print  for i := 0 to 7 do  begin    for j := 0 to 7 do      Write(Arr[j][i]);    Writeln;  end;   // Memory freed autoend. ---------------------------------------------------------------------- 440bx · 2025-02-01 01:24 @ASerge, your FPC rendition is really a _lot_ cleaner than the C way.