From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Ada array contiguity. Date: Sun, 19 Feb 2023 16:59:42 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net 1SYy15ddI+kyyoR1I+Ny2wYeJ4fHV/WlQUOIv2eWyZilndNHQr Cancel-Lock: sha1:L4+34yDxBOQ4jU81429lMWYlw2E= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Content-Language: en-US In-Reply-To: Xref: reader01.eternal-september.org comp.lang.ada:64943 List-Id: On 2023-02-19 16:28, J-P. Rosen wrote: > Le 19/02/2023 à 14:34, Rod Kay a écrit : >> Hi all, >> >>     I've been told that Ada array elements are not guaranteed to be >> contiguous unless the 'Convention C' aspect is applied. >> >>     Is this correct ? > > The strength of Ada is that it protects you from all implementation > details, thus allowing compilers to choose the most efficient > implementation. Therefore, the answer is yes. I tried to find a rule on "contiguity" in the Ada 2022 RM, but failed. Can you point to one? Perhaps this rule is a consequence of C standard rules for arrays (pointer arithmetic), and the general idea that Ada should allow Convention C for a type only if that type is really compatible with the C compiler (in question). For a constrained array type I would choose to specify the size of the component type, and the size of the array type to be the length of the array times the component size. That should (also) ensure that the elements are stored contiguously (if the Ada compiler accepts this size specification). It seems (RM B.3(62.4/3)) that Ada compilers are not required to support Convention C for unconstrained array types. RM B.3 (Interfacing with C/C++) declares such types with the Pack aspect, but that may or may not (AIUI) give a contiguous representation. > (BTW: try to find a definition of "contiguous". At byte level? At word > level? What if the element does not fill a byte?) Indeed. But it seems to me that Arr'Size = Arr'Length * Comp'Size is the meaning usually intended for programming purposes.