This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: C++ and Java CNI: Check Java references
Fergus Henderson <fjh@cs.mu.oz.au> writes:
> On 24-Apr-2001, Andrew Haley <aph@cambridge.redhat.com> wrote:
> > > It's also common to write expressions such as `&p[0]',
> >
> > I think it's impossible in this case. These are Java objects, and
> > C++-style arrays can't occur.
>
> In C++, any object is equivalent to a length one array of itself.
>
> So library code may handle the single-object case by calling the code
> for the array case with an array size of one.
>
> You might, for example, pass a pointer to a Java object to some
> library template routine that uses &p[0] before checking whether
> p is null:
>
> template <class T>
> void foo(T *p, int n) {
> void *raw_pointer = &p[0];
> if (raw_pointer != NULL && n != 0) { ... }
> }
>
> template <class T>
> void foo(T *p) {
> foo(p, 1);
> }
>
> Such code isn't strictly conforming, but it works on all traditional
> implementations, which don't explicitly check for dereferencing null,
> and for which no actual dereference takes place if the dereferencing
> operator is the operand of unary `&'.
>
> OK, maybe this is a bit far-fetched. It's unlikely to be a big
> problem in practice, I guess.
In C at least the ISO C99 standard was explicitly modified to allow
this to work, so apparently someone uses it. The equivalent
void foo(char *p, int n) {
void *raw_pointer = &p[0];
if (raw_pointer != NULL && n != 0) { ... }
}
is strictly conforming C.
It's likely this change will move into C++ in the next standard revision.
--
- Geoffrey Keating <geoffk@geoffk.org>