comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@home.com>
Subject: Re: type Foo_ptr in new void*;
Date: Mon, 30 Jul 2001 02:36:02 GMT
Date: 2001-07-30T02:36:02+00:00	[thread overview]
Message-ID: <3B64C812.8CA53003@home.com> (raw)
In-Reply-To: 3B64C26F.C195B4E0@worldnet.att.net

James Rogers wrote:
> Tomasz Wegrzanowski wrote:
> > In article <3B63F48A.2E2642C6@earthlink.net>, Marc A. Criley wrote:
> > >> I can't do this.
> > >> Foo is supposed to be used only as a pointer.
> > >> I don't know if newer version of library will use the same Foo.
> > >
> > > In tmoran's post, wherever you saw "in out Foo", change it to "in
> > > Foo_Pointer".
> > >
> > > In order to declare a pointer to a type, you have to have that type's
> > > definition.  Even C requires that for "foo*" there be some declaration
> > > of "foo".
> >
> > Not true.
> > You can use `struct Foo *' without declaring `struct Foo'.
> > You can `typedef struct Foo Foo_t;' or `typedef struct Foo *Foo_p;'
> > and use Foo_t and Foo_p w/o declaring `struct Foo'.
> 
> Yes, you are correct. C does allow you to forward declare a structure
> or a pointer without ever completing the definition. This is one of
> the nasty capabilities "provided" by C. You can even create an array
> of Foo *. However, pointer arithmetic from the start of this array
> produces unspecified behavior. What is the sizeof Foo? Since it is
> undefined, there is no correct answer to that question. Without a
> proper definition of the sizeof Foo, there can be no reliable pointer
> arithmetic.

I don't think your "unspecified behavior" assertion is truly fair here.
I believe that when you reach a point in the C/C++ compile where 
pointer arithmetic is required on the "undefined type", you will get
a compile error. The compiler will not generate code that does 
"undefined behaviour" here. If there is any further doubt about it,
I am sure this can be tested with a short example program.

> In other words, you can declare some wonderful data structures in C
> based on incomplete type definitions. Unfortunately, a program built
> upon such a structure is neither reliable nor portable.

Sad, but true. What sometimes happens, is that the C programmer wants
the application to see an "opaque type", and so presents a different
type to the user (by include file) than the original structure. If
this "proxy" is incorrectly defined, its size can be wrong, causing
of course, much "havoc" under the right circumstances. Ada of course,
provides a better model for visibility.

> Ada does not allow you to compile a program with an incomplete type
> definition. There is no equivalent because the existence of an
> equivalent to this C capability would only introduce errors in your
> program. There is nothing positive that can be achieved by allowing
> a program to be incompletely specified.

Ada does permit you to define an incomplete type as well, but the
condition is that it must be defined eventually. I agree that enforcing
its later definition, is a good thing.

-- 
Warren W. Gay VE3WWG
http://members.home.net/ve3wwg



  reply	other threads:[~2001-07-30  2:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-29  4:31 type Foo_ptr in new void*; Tomasz Wegrzanowski
2001-07-29  5:56 ` tmoran
2001-07-29  8:57   ` Tomasz Wegrzanowski
2001-07-29 12:26     ` Marc A. Criley
2001-07-30  1:05       ` Tomasz Wegrzanowski
2001-07-30  1:20         ` tmoran
2001-07-30  2:09         ` James Rogers
2001-07-30  2:36           ` Warren W. Gay VE3WWG [this message]
2001-07-30  3:10             ` James Rogers
2001-07-31  2:14               ` Warren W. Gay VE3WWG
2001-07-31  1:21           ` Tomasz Wegrzanowski
2001-07-31  3:06             ` James Rogers
2001-07-31  5:02               ` Warren W. Gay VE3WWG
2001-07-31  7:22               ` Florian Weimer
2001-07-31  4:56           ` Darren New
2001-08-04  6:05             ` David Thompson
2001-08-05  2:27               ` Warren W. Gay VE3WWG
2001-07-29 13:40     ` Dale Stanbrough
2001-07-29 14:12 ` Florian Weimer
replies disabled

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