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,aeab6b16387b2612 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-07-19 22:39:13 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!feed.textport.net!news.stealth.net!204.127.161.2.MISMATCH!wn2feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <9itna2$i5b$1@news.tpi.pl> Subject: Re: How to do it in Ada ? X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <5uP57.44733$C81.3757901@bgtnsc04-news.ops.worldnet.att.net> Date: Fri, 20 Jul 2001 05:39:13 GMT NNTP-Posting-Host: 12.89.131.209 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 995607553 12.89.131.209 (Fri, 20 Jul 2001 05:39:13 GMT) NNTP-Posting-Date: Fri, 20 Jul 2001 05:39:13 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:10303 Date: 2001-07-20T05:39:13+00:00 List-Id: Mark Lundquist wrote : ... > Things to note: > > 1) No heap allocation is required. In C, the size in an array > declaration _has_ to be static. So in the original C example, the use of > malloc() is necessitated by the fact that we don't know at compile time how > many elements the array will have. In Ada, you can declare an object with a > non-static constraint. ... The recent revision C99 added, and gcc/GNU-C has long implemented as an extension, "auto" arrays and array types with bounds computed effectively at elaboration (although they don't call it that), somewhat unimaginatively called Variable Length Arrays. (They are still 0-based and not bounds-checked.) > 2) The "(others => 0)" thing is called an aggregate. Aggregates are to > composite types (arrays and records) what literals are to elementary types, > so you can use an aggregate to initialize an object of a composite type. So > for arrays, there is no need to code an iteration across the array elements. For a declared array in C, you can write an initializer which specifies only some, at minimum one, element(s), and all unspecified elements are initialized to 0 (or floating 0.0 or pointer NULL). In C99 you can select arbitrary elements using a Designated Initializer: int foo[10] = { [1] = 3, [7] = 4 }; /* [0,2..6,8,9] = 0 */ but you cannot use ranges (3..5 => 42) or have the "remainder" (others =>) case be anything other than "zero" as above. malloc'ed space is not initialized, except that by using calloc instead you can have it initialized to all-zero-BITS; this is not guaranteed to produce integer 0, floating 0.0, or pointer NULL, with the single exception of the type unsigned char, though on most if not all "normal" systems it does. Also on many popular current OSes all memory not explicitly (statically) loaded is initialized to zero-bits, including not-yet-used stack and heap, but to use this "safely" you must have control over all the code in your process from main down and even then it's easy to get wrong. Also new in C99 you can write a Compound Literal for an array or struct. But arrays still aren't first-class; you can't assign any array value, including such a literal, to an array object/variable, malloc'ed or not. You can memcpy() it, but this is easier to mess up. > Not only does this mean that you're programming at a higher level of > abstraction, it leaves freedom for the compiler to do it more efficiently, > if the compiler knows a better way then elementwise iteration. > Well, even if you write the loop, a good compiler can optimize it to something better. The important gain is source clarity. -- - David.Thompson 1 now at worldnet.att.net