0

This code accepts data in a static array.

TMyRec = record
   MyArray: array[0..1, 0..10] of double;
end;

MyClient: TIdUDPClient;
MyRec: TMyRec;
Buffer: TIdBytes;

SetLength(Buffer, SizeOf(MyRec));
if MyClient.ReceiveBuffer(Buffer, 1) > 0 then
begin
     BytesToRaw(Buffer, MyRec, SizeOf(MyRec));
end;

And how do it in a dynamic array.

TMyRec = record
   MyArray: array of array of double;
end;
rustam
  • 21
  • 4
  • That code does not compile – David Heffernan Feb 26 '19 at 17:16
  • As @David says, your code does not compile. One example of something that doesn't make any sense: you cannot SetLength a record, and Buffer is not a valid length. Programming is not a matter of throwing things to a wall and hoping that something sticks. If you use a function, you should know what parameters it expects. If you don't understand what they mean, you should perhaps start with something simpler. – Rudy Velthuis Feb 26 '19 at 17:51
  • that's the question – rustam Feb 26 '19 at 18:00
  • @Rustam: **what's the question** exactly? That it doesn't compile? Then take a look at the error message (you didn't post any) and see what it says. Usually the text of an error message is a clear hint on what went wrong. And, if possible, post a complete program that displays the problem, so people can try things out themselves. – Rudy Velthuis Feb 26 '19 at 18:04
  • compile is not necessary, it is a brief information, you just need to understand – rustam Feb 26 '19 at 18:07
  • "This code accepts data in a static array". No it doesn't. This question doesn't meet the required criteria for an SO question. It will soon be closed unless you try to fix the problems. – David Heffernan Feb 26 '19 at 18:17
  • it is now clearer – rustam Feb 26 '19 at 18:26
  • It is for voting readers to decide whether a q is clear (or clearer). – MartynA Feb 26 '19 at 19:25

1 Answers1

1

First off, you are allocating Buffer to SizeOf(MyRec) bytes (176 for the static array version), but then you are only reading 1 byte from the UDP socket. You need to replace 1 with SizeOf(MyRec) or Length(Buffer) instead, to match the allocation.

That being said, an array of array of ... is not stored in one contiguous block of memory. It is actually an array of pointers to other arrays that are scattered all over memory. So, to do what you are asking, you would have to do something like this:

type
  TMyRec = record
    MyArray: array of array of double;
  end;

const
  BytesPerArr = SizeOf(Double) * 11;

var
  MyClient: TIdUDPClient;
  MyRec: TMyRec;
  Buffer: TIdBytes;

...

SetLength(MyRec.MyArray, 2, 11);
SetLength(Buffer, 2 * BytesPerArr);

if MyClient.ReceiveBuffer(Buffer, Length(Buffer)) > 0 then
begin
  Move(Buffer[0], MyRec.MyArray[0][0], BytesPerArr);
  Move(Buffer[BytesPerArr], MyRec.MyArray[1][0], BytesPerArr);
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • if I do not know what size sent UdpServer ( I mean 2, 11). – rustam Feb 26 '19 at 19:34
  • 1
    WHY do you not know the size being sent? Doesn't your UDP communication use a defined protocol for its data? It should. But, if you really don't know, then your only option is to allocate the `Buffer` to the max byte size that UDP can physically transmit over IPv4 (65507 bytes - most people just round it up to 65535 bytes for convenience) and then use the return value of `ReadBuffer()` to know how many bytes were actually received, and then divide that by the number of `Double`s/arrays you are expecting. – Remy Lebeau Feb 26 '19 at 21:34
  • 1
    For instance, assuming 11 `Double`s per array, you can calculate the number of arrays in the data by dividing the return value of `ReadBuffer()` by `11 * SizeOf(Double) = 88` bytes. But, if you don't know how many `Double`s to expect per array, then you would need to know how many arrays to expect, in which case you can then divide the return value by that to get the number of `Double`s per array. Otherwise, if you don't have that, then you are SOL without a protocol explicitly telling you how many arrays and `Double`s per array are actually being sent. – Remy Lebeau Feb 26 '19 at 21:35
  • What does the server's raw data actually look like? – Remy Lebeau Feb 26 '19 at 21:35
  • Server sends the same dynamic array. I realized that I needed to set the size of the array and could not do without it. Thank you very much for the detailed answers. – rustam Feb 27 '19 at 04:29