Re: G_ARRAY_LENGTH for glib.h?



On 21 Jan 2000, Guillaume Laurent wrote:

> Date: 21 Jan 2000 21:31:23 +0100
> From: Guillaume Laurent <glaurent@worldnet.fr>
> Reply-To: gtk-devel-list@redhat.com
> To: gtk-devel-list@redhat.com
> Subject: Re: G_ARRAY_LENGTH for glib.h?
> Resent-Date: 21 Jan 2000 20:31:35 -0000
> Resent-From: gtk-devel-list@redhat.com
> Resent-cc: recipient list not shown: ;
> 
> David Benson <daveb@idealab.com> writes:
> 
> > #define G_IS_STATIC_ARRAY(array)  ((void*)&(array) == (void*)&((array[0])))
> > 
> > is almost dangerously clever, but works I think....
> 
> AFAICT, this will always be true. Perhaps did you mean
> 
>  array == &array 
> 
> which will be true for static arrays ?

That expression will always trigger a constraint violation; you still
need the cast.

Anyway, the address of the first element of an array is the same as
the address of the whole array. So this test will work for an array
regardless of its storage class. Even for dynamically allocated
arrays.

What the G_IS_STATIC_ARRAY does is really test whether the item is an
array or a pointer.

It should be called:

    G_IS_ARRAY_TYPE_RATHER_THAN_POINTER(A) ((void *) &(A) == (void *) &(A)[0])

If it is a pointer, then its address is distinct from the location of the
first element that it points to.

Of course, this test is not completely reliable. I can create a pointer which
will make the test true: namely a pointer which points to itself.

    char *p;

    p = (char *) &p;

So the name should really be G_EXPR_POINTS_TO_ITSELF() .

ROTFL.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]