From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,dcfafdc50abb8ce4,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-06 13:01:02 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.algonet.se!algonet!pepsi.tninet.se!not-for-mail From: Mats Karlssohn Newsgroups: comp.lang.ada Subject: records containing variable length arrays [long] Date: Wed, 06 Jun 2001 21:59:53 +0200 Organization: MIDA Systemutveckling AB Message-ID: <3B1E8BB9.42BF95D2@mida.se> NNTP-Posting-Host: sdu148-250.ppp.algonet.se Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: cubacola.tninet.se 991857499 11531 195.163.250.148 (6 Jun 2001 19:58:19 GMT) X-Complaints-To: abuse@algo.net NNTP-Posting-Date: 6 Jun 2001 19:58:19 GMT X-Mailer: Mozilla 4.77 [en] (WinNT; U) X-Accept-Language: en Xref: archiver1.google.com comp.lang.ada:8256 Date: 2001-06-06T19:58:19+00:00 List-Id: I'm currently wrestling with an interesting problem. I feel that this have probably been discussed previously, but I couldn't find anything on it neither on AdaPower nor on the cla. archives on google. I would like to declare a record type containing two (possibly more) arrays of variable lengths, the length of each array is another element of the record. I also need to specify an exact representation for the record since it is a message that I cant change the format of. A piece of code to illustrate (please not that this is NOT compilable) package Communication is -- Basic types type Byte is mod 2**8; for Byte'size use 8; type Word is mod 2**16; for Word'size use 16; -- Variable length buffer type Buffer is array(Byte range <>) of Byte; pragma Pack(Buffer); -- Declaring the variable length elements is one major headache -- note that at least Output_Length may legally be 0 type Message is record Magic : Word; Operation : Word; Status : Word; Response_Length : Byte; Output_Length : Byte; Response_Data : Buffer(0..(Response_Length - 1)); Output_Data : Buffer(0..(Output_Length - 1)); CRC : Word; end record; -- the other big problem is to get the correct representation for Message use record Magic at 0 range 0..15; Operation at 2 range 0..15; Status at 4 range 0..15; Response_Length at 4 range 0.. 7; Output_Length at 5 range 0.. 7; Response_Data at 6 range 0..(Response_Length * Byte'size - 1); Response_Data at (6 + Response_Length) range 0..(Output_Length * Byte'size - 1); CRC at (6 + Response_Length + Output_Length) range 0..15; end record; end Communication; The compiler (Gnat 3.13p on Linux) says: $ gnatgcc -c -g -gnata -gnatf communication.ads communication.ads:23:39: component "Response_Length" cannot be used before end of record declaration communication.ads:24:39: component "Output_Length" cannot be used before end of record declaration communication.ads:34:11: component "Response_Length" overlaps "Status" at line 33 communication.ads:35:11: component "Output_Length" overlaps "Status" at line 33 communication.ads:36:42: "Response_Length" is undefined communication.ads:37:35: "Response_Length" is undefined communication.ads:37:62: "Output_Length" is undefined communication.ads:38:35: "Response_Length" is undefined communication.ads:38:53: "Output_Length" is undefined $ I have currently solved this by overlaying the record with a large enough array of Byte, and then calculating what portions of that I must use Unchecked_Conversion on to find the data I need. This works BUT: * It is ugly and I would like to do this as as much of a schoolexample that I can possibly do. * Performance may be a bottleneck (probably not in the particular case above) so I would really like to avoid copying any of the elements. Any suggestions ? Thanks for any help! -- Mats Karlssohn, developer mailto:mats@mida.se Mida Systemutveckling AB http://www.mida.se Box 64, S-732 22 ARBOGA, SWEDEN Phone: +46-(0)589-89808 Fax: +46-(0)589-89809