Delphi problem writing an array

Help

How can I write an array that was declare in an object? If the array is locally declared there is not problem, it works perfect. However, if the array is part of an object it result in garbage; see below:

Tsi16 = class(Thdr) // signed integer 16b (smallInt)
dta : arraySI16; // dataset
tbl : Tlist; // tables
end;

procedure saveHDFgrid(fNm: pAnsiChar;grd: Tgrid); stdcall;
var
ii : integer;
lyrN : ansiString;
xxx : THDF5Dll;
hFile : hid_t; // file handle (ID)
hDtSp : hid_t; // data space handle (ID)
hDtSt : hid_t; // dataset hadle (ID)
status : herr_t; // hdf calling status
aDim : array of hsize_t; // array dimension specification
ptr : pointer;
hAtt : hid_t;

kk,jj: integer;
xx: array [0…39,0…39] of smallInt;
begin
xxx:= THDF5DLL.Create(extractFilePath(paramStr(0)) + ‘hdf5.dll’);
hFile:= xxx.H5Fcreate(fNm, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

for ii:= 0 to grd.lyr.Count - 1 do begin
// ------------------------------------------------ Create the dataset space
setLength(aDim,2);
aDim[0]:= Thdr(grd.lyr[ii]).gHdr.nRows; // array definition
aDim[1]:= Thdr(grd.lyr[ii]).gHdr.nCols; // array definition
hDtSp:= xxx.H5Screate_simple(2,pHsize_t(@aDim[0]),nil); // data space
// ------------------------------------------------------ Create the dataset
lyrN:= ansiString(Thdr(grd.lyr[ii]).band);
hDtSt:= xxx.H5Dcreate2(hFile,pAnsiChar(lyrN),xxx.H5T_INTEL_I16,hDtSp,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
// ------------------------------------------------------- Write the dataset

case grd.lyrDT[ii] of
  UI08:;
  SI16: ptr:= Tsi16(grd.lyr[ii]).dta;
  UI16:;
  SI32:;
  UI32:;
  F32 :;
  F64 :;
  UKN :;
end;

// THIS work PERFECTLY -----------------------------------------------------
for kk:= 0 to 39 do
  for jj:= 0 to 39 do
    xx[kk,jj]:= kk;

status:= xxx.H5Dwrite(hDtSt,xxx.H5T_INTEL_I16,H5S_ALL,H5S_ALL,H5P_DEFAULT,@xx);
// -------------------------------------------------------------------------

// THIS is NOT working -----------------------------------------------------
status:= xxx.H5Dwrite(hDtSt,xxx.H5T_INTEL_I16,H5S_ALL,H5S_ALL,H5P_DEFAULT,ptr);
// -------------------------------------------------------------------------


// ------------------------------------------------------------------- Close
status:= xxx.H5Dclose(hDtSt);   // End access to the dataset and release resources
status:= xxx.H5Sclose(hDtSp);  // Terminate access to the data space
status:= xxx.H5Fclose(hFile);    // Close the file

end;
end;

Hi Jorge!

28.04.2018 0:45, Jorge Guzman пишет:

How can I write an array that was declare in an object? If the array is
locally declared there is not problem, it works perfect. However, if the
array is part of an object it result in garbage; see below

Tsi16 = class(Thdr) // signed integer 16b (smallInt)
dta : arraySI16; // dataset
tbl : Tlist; // tables
end;

I’m now sure what is arraySI16 (array of SmallInt ?), but generally
class fields should work not much differently differently from local
variables.

Depending on how arraySI16 is actually defined, I would expect something
like either
ptr := PSmallInt(Tsi16(grd.lyr[ii]).dta)
or
ptr := @Tsi16(grd.lyr[ii]).dta
to work.

Best wishes,
Andrey Paramonov

Thank you Andrey. I think I solve the issue. When using 2D dynamic arrays we need to write row by row. Here is the code:

// -----------------------------------------------------------------------------
// save a set of 2D Grid (array) to an HDF file
// mode :
// -----------------------------------------------------------------------------

procedure saveHDFgrid(fNm: pAnsiChar;grd: Tgrid); stdcall;
var
ii,nn : integer; //
lyrN : ansiString; // layer
xxx : THDF5Dll; // HDF
hFile : hid_t; // file handle (ID)
hDtSp : hid_t; // data space handle (ID)
hDtSt : hid_t; // dataset handle (ID)
hMmSp : hid_t; // memory space handle (ID)
fDim : array[0…1] of hsize_t; // array dimension specification
mDim : hsize_t; // memory dimension specification
fHslb : T2DhyperSlab; // file hypperSlab specification
mHslb : T1DhyperSlab; // memory hypperSlab specification
ptr : pointer; // pointer to dataset row
stts : herr_t; // HDF calling status
begin
xxx:= THDF5DLL.Create(extractFilePath(paramStr(0)) + ‘hdf5.dll’);
hFile:= xxx.H5Fcreate(fNm,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);// file handle

for ii:= 0 to grd.lyr.Count - 1 do begin
// ------------------------------------------------ Create the dataset space
fDim[0]:= Thdr(grd.lyr[ii]).gHdr.nRows; // array definition
fDim[1]:= Thdr(grd.lyr[ii]).gHdr.nCols; // array definition
hDtSp:= xxx.H5Screate_simple(2,pHsize_t(@fDim[0]),nil); // file dataSpace
// ------------------------------------------------------ Create the dataset
lyrN:= ansiString(Thdr(grd.lyr[ii]).band);
hDtSt:= xxx.H5Dcreate2(hFile, // file dataset handle
pAnsiChar(lyrN),
xxx.H5T_INTEL_I16,
hDtSp, // file data space handle
H5P_DEFAULT,
H5P_DEFAULT,H5P_DEFAULT);
// -------------------------------------------------------- Memory HyperSlab
mDim:= Thdr(grd.lyr[ii]).gHdr.nCols;
hMmSp:= xxx.H5Screate_simple(1,@mDim,nil); // memory data space handle

mHslb.start :=  0;                            // offset
mHslb.count :=  Thdr(grd.lyr[ii]).gHdr.nCols; // count
mHslb.stride:=  1;                            //
mHslb.block :=  1;                            //

stts:= xxx.H5Sselect_hyperslab(hMmSp,         // hyperslab in memory dataset
                               H5S_SELECT_SET,
                               pHsize_t(@mHslb.start),
                               pHsize_t(@mHslb.stride),
                               pHsize_t(@mHslb.count),
                               pHsize_t(@mHslb.block));
// ---------------------------------------------------------- File HyperSlab
fHslb.start[0] :=  0;                         // row offset
fHslb.start[1] :=  0;                         // columns offset
fHslb.count[0] :=  1;                         // rows count
fHslb.count[1] := Thdr(grd.lyr[ii]).gHdr.nCols;  // columns count
fHslb.stride[0]:=  1;                         //
fHslb.stride[1]:=  1;                         //
fHslb.block[0] :=  1;                         //
fHslb.block[1] :=  1;                         //
// ------------------------------------------------------- Write the dataSet
for nn:= 0 to Thdr(grd.lyr[ii]).gHdr.nRows - 1 do begin
  fHslb.start[0]:= nn;
  stts:= xxx.H5Sselect_hyperslab(hDtSp,       // file hyperSlab
                                 H5S_SELECT_SET,
                                 pHsize_t(@fHslb.start[0]),
                                 pHsize_t(@fHslb.stride[0]),
                                 pHsize_t(@fHslb.count[0]),
                                 pHsize_t(@fHslb.block[0]));

  case grd.lyrDT[ii] of                       // ptr to contiguous row data
    UI08: ptr:= Tui08(grd.lyr[ii]).dta[nn];
    SI16: ptr:= Tsi16(grd.lyr[ii]).dta[nn];
    UI16: ptr:= Tui16(grd.lyr[ii]).dta[nn];
    SI32: ptr:= Tsi32(grd.lyr[ii]).dta[nn];
    UI32: ptr:= Tui16(grd.lyr[ii]).dta[nn];
    F32 : ptr:= Tf32(grd.lyr[ii]).dta[nn];
    F64 : ptr:= Tf64(grd.lyr[ii]).dta[nn];
    UKN : ptr:= Tf64(grd.lyr[ii]).dta[nn];
  end;

  stts:= xxx.H5Dwrite(hDtSt,                  // dataset handle
                      xxx.H5T_INTEL_I16,      // data type
                      hMmSp,                  // memory space handle
                      hDtSp,                  // file data space handle
                      H5P_DEFAULT,            //
                      ptr);                   // data
end;
// ------------------------------------------------------------------- Close
stts:= xxx.H5Dclose(hMmSp); // Terminate access & release resources
stts:= xxx.H5Dclose(hDtSt); // Terminate access & release resources
stts:= xxx.H5Sclose(hDtSp); // Terminate access to the data space

end;
stts:= xxx.H5Fclose(hFile); // Close the file
end;