Re: G_ARRAY_LENGTH for glib.h?
- From: David Benson <daveb idealab com>
- To: gtk-devel-list redhat com
- cc: Darin Adler <darin bentspoon com>, Tim Janik <timj gtk org>
- Subject: Re: G_ARRAY_LENGTH for glib.h?
- Date: Fri, 21 Jan 2000 08:16:08 -0800 (PST)
> > An aside: In the many years that I have used a macro like this, the most
> > common mistake people made was to change code from using an array to using a
> > dynamically allocated array; my ARRAY_LENGTH macro would then evaluate to 1.
> > It would be better if it just didn't compile. In C++, there's a way to do
> > this with a function template; it just won't compile if you pass it a
> > pointer. I'd love to find a way to do this in C with some GCC extension.
>
> Hmmm, I can't think of any way of doing that. sizeof() is about the only
> operator in C that distinguishes an array from a pointer to the array
> element. (GCC adds typeof(), but I don't think it helps.)
#define G_IS_STATIC_ARRAY(array) ((void*)&(array) == (void*)&((array[0])))
is almost dangerously clever, but works I think....
- Dave
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]