comp.lang.ada
 help / color / mirror / Atom feed
From: "David Thompson" <david.thompson1@worldnet.att.net>
Subject: Re: How to do it in Ada ?
Date: Fri, 20 Jul 2001 05:39:13 GMT
Date: 2001-07-20T05:39:13+00:00	[thread overview]
Message-ID: <5uP57.44733$C81.3757901@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: P0F47.357734$p33.7248200@news1.sttls1.wa.home.com

Mark Lundquist <up.yerz@nospam.com> 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








  parent reply	other threads:[~2001-07-20  5:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-16  3:33 How to do it in Ada ? Tomasz Wegrzanowski
2001-07-16  5:31 ` Jeffrey Carter
2001-07-16  6:41 ` Al Christians
2001-07-16  6:51 ` tmoran
2001-07-16 16:56   ` Mark Lundquist
2001-07-16 18:42     ` Nonsense (was Re: How to do it in Ada ?) Mark Lundquist
2001-07-16 22:20       ` Jeffrey Carter
2001-07-17  0:13       ` Ken Garlington
2001-07-17  3:53       ` Robert Dewar
2001-07-16 22:18     ` How to do it in Ada ? Jeffrey Carter
2001-07-17  4:06       ` tmoran
2001-07-20  5:39     ` David Thompson [this message]
2001-07-16 17:37   ` Ken Garlington
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox