C++ and Java CNI: Check Java references

Fergus Henderson fjh@cs.mu.oz.au
Tue Apr 24 11:37:00 GMT 2001

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.

Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: < http://www.cs.mu.oz.au/~fjh >  |     -- the last words of T. S. Garp.

More information about the Java mailing list