Re: G_ARRAY_LENGTH for glib.h?

Darin Adler <> writes:

> > hm, shouldn't that rather be G_N_ELEMENTS() ?
> G_ELEMENTS does sound like something that returns a pointer to some
> elements, not a count of elements. So I, for one, prefer G_N_ELEMENTS.

OK, I've added it as G_N_ELEMENTS.

> One reason to prefer a name that includes the word "array" is to emphasize
> the fact that this does not work properly when passed a non-array pointer.
If we wanted a name including ARRAY, then it would have to be somethign


But I don't expect that really helps that much. If you understand the
distinction between arrays and pointers in C, then you won't make
the mistake. If you don't understand the distinction it won't help.
> 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.)


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