M
M
Maxim2016-09-27 14:01:01
Delphi
Maxim, 2016-09-27 14:01:01

How are multidimensional arrays filled?

I am rewriting a program from delphi to c#, I have no experience with delphi. I ran into a problem, I don’t understand how the mkts and msh array is filled . I would appreciate any help.

mkts = array [0..3, 1..6, 1..16] of SmallInt;
msh = array[1..392] of SmallInt;

TPmkts = ^mkts;
TPmsh = ^msh;

Pkts: array[1..Blocks, 1..RTParts] of TPmkts = ( (nil, nil),
                                (nil, nil), (nil, nil), (nil, nil) );
Psh: array[1..4, 1..2] of TPmsh = ( (nil, nil),
                                (nil, nil), (nil, nil), (nil, nil) );
...
Df: array[1..4, 1..2] of integer;

{------------------------------------------------------------------------}

function OpenA(FileName: ShortString; Mode: LongWord): integer;
begin
  Result:= FileOpen(FileName, Mode);
end;

{------------------------------------------------------------------------}

function ReadA(Df: integer; Pmas: Pointer; L: word): boolean;
begin
  Result:= false;
  if FileSeek(Df, 0, 0) = 0 then
    if FileRead(Df, Pmas^, L) > 0 then
      Result:= true;
end;
...

{------------------------------------------------------------------------}

Df[BlNum, ROTO]:= OpenA(ShifrFileName, fmShareDenyNone);

{ Выделение памяти }
New(Psh[4, 1]);

if not ReadA(Df[4, 1], Psh[4, 1], 784) then
begin
  CloseA(Df[4, 1]);
  Dispose(Psh[4, 1]);
  Exit;
end;

Pkts[4, 1]:= @Psh[4, 1]^[9];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-09-27
@viqking

New(Psh[4, 1]);
We translate into C#. True, there is one catch. Arrays in c# start only from 0, and you need to look: either start a blank 0th element, or adjust the indices. So we have two options.

Psh[4][1] = new short[393];
Psh[3][0] = new short[392];
(for example, I work with the second)
function OpenA(FileName: ShortString; Mode: LongWord): integer;
Stream OpenA(string FileName, FileAccess access);

How to translate ReadA? It's not so easy. The fact is that languages ​​with standard intermediate code (primarily the Java and .NET languages) are usually written in Motorola byte order. On Delphi the file was written in the order of Intel. So we read 392 words, and then we see what to do: either wrap the bytes, or say: now we have a new standard - Motorola!
if (!ReadA(Df[3][0], Psh[3][0], 392) {
  CloseA(Df[3, 0]);
  Psh[3][0] = null;
  return;
}

And the last push.
Due to the lack of pointers (more precisely, there are, but we don’t want unsafe code), Pkts will have to be split into two parts: a reference to an array and an offset in the array.
Pkts[3][0].array = Psh[3][0];
Pkts[3][0].offset = 8;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question