comp.lang.ada
 help / color / mirror / Atom feed
From: ka@felix.UUCP (Kenneth Almquist)
Subject: Re: Reference vs. copy semantics in passing parameters
Date: 28 May 91 05:28:29 GMT	[thread overview]
Message-ID: <163815@felix.UUCP> (raw)
In-Reply-To: 1991May16.135103.1688@software.org

>In article <2742@sparko.gwu.edu> mfeldman@seas.gwu.edu () writes:
>> In C++, you can declare not only the pointer constant but the pointed
>> to construct constant as well. This allows passing by reference in a
>> read-only manner, which is NOT possible in Ada at present.

This feature has also been included in the ANSI C standard.

blakemor@software.org (Alex Blakemore) asks:
> This sounds like a nice safety feature but can callers really rely on it ?
> Even if the C++ language prevents updating the object if the pointer is
> declared appropriately, does it prevent assignment to a normal pointer
> which will allow the referenced object to be updated ?

The "constant" attribute is part of the type, so the type checking system
makes this secure *unless* the type system is circumvented by the use of
a type cast.  (By "type cast", I mean the C equivalent of Ada's
UNCHECKED_CONVERSION.)  The rule is that a pointer to a non-constant
object can be assigned to a pointer to a constant object, but not the
other way around.  (Assignment includes parameter passing.)

A problem with the "constant" attribute is that it is not inherited
properly.  Consider a routine that looks up an entry in a data
structure like a tree or a string, and returns a pointer to the
element found.  The returned pointer should point to a constant object
iff the data structure was declared to be constant, but the C type
system doesn't handle this.  For this reason, several routines in the
standard ANSI C library are defined in such a way that they cannot be
implemented without using type casts.
					Kenneth Almquist

  parent reply	other threads:[~1991-05-28  5:28 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-02-11 15:06 ADA Compiling Query Gordon Russell
1991-02-12 19:01 ` Michael Feldman
1991-02-13 21:16 ` Jeff Bartlett
1991-02-14 16:45   ` Michael Feldman
1991-02-15 23:09     ` Jim Showalter
1991-02-17  0:19       ` Reference vs. copy semantics in passing parameters Michael Feldman
1991-02-17 18:54         ` Erland Sommarskog
1991-02-18 18:41           ` Doug Smith
1991-02-18  0:36         ` Jim Showalter
1991-02-18  1:42           ` Michael Feldman
1991-02-18 18:49           ` Arthur Evans
1991-02-19  2:05             ` Michael Feldman
1991-02-18 13:10         ` madmats
1991-02-19 19:00           ` Robert I. Eachus
1991-05-16 13:51         ` Alex Blakemore
1991-05-17  8:19           ` Matthias Ulrich Neeracher
1991-05-17 13:44           ` Markku Sakkinen
1991-05-28  5:28           ` Kenneth Almquist [this message]
1991-02-18 14:33 ` ADA Compiling Query stt
replies disabled

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