Re: G_ARRAY_LENGTH for glib.h?

On Fri, 21 Jan 2000, David Benson wrote:

> > I think that the runtime check is better than no check at all. That problem
> > with getting 1 is not a theoretical one. It has happened to me a lot. Here's
> > another variation:
> > 
> >     #define G_N_ELEMENTS(array) (G_IS_STATIC_ARRAY(array) \
> >         ? (g_error("G_N_ELEMENTS called on non-array"), 0) \
> >         : sizeof(array) / sizeof((array)[0])
> > 
> > Since this doesn't use g_assert, it would need to be inside a
> > G_DISABLE_ASSERT wrapper, or have its own symbol to disable the check and
> > fall back to the simpler version.
> > 
> >     -- Darin

huh, do we have mailing list problems? i don't see the original mail
for this.

in any case, the above simply doesn't work for initialisations that
are performed outside of a function's scope, where you can't call
other functions.

owen has added:

#define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))

to glib.h, and i'm pretty convinced we should leave it at that.

> I don't think it has to be inside a G_DISABLE_ASSERT wrapper
> very much since the g_error should be entirely optimized
> away by the compiler.

that's btw not always true, even with high optimizations turned on,
code like

if (0)
  some_func ();

will still cause references to some_func() end up in the object file
(even for implicit declarations).

> - Dave


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