[GRASS-dev] G_gettext initialization

In G_gettext (lib/gis/locale.c), I ran across this code:

char *
G_gettext(const char *package, const char *msgid)
{
#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
    static char now_bound[4096];
    static int initialized;

    if (!initialized)
    {
        ...
        initialized = 1;
    }
...
}

Is there any particular reason that 'initialized' is not actually
initialized? Is there a reason for this?

--
Brad Douglas <rez touchofmadness com> KB8UYR/6
Address: 37.493,-121.924 / WGS84 National Map Corps #TNMC-3785

Brad Douglas wrote:

In G_gettext (lib/gis/locale.c), I ran across this code:

char *
G_gettext(const char *package, const char *msgid)
{
#if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
    static char now_bound[4096];
    static int initialized;

    if (!initialized)
    {
        ...
        initialized = 1;
    }
...
}

Is there any particular reason that 'initialized' is not actually
initialized? Is there a reason for this?

I don't understand what you are asking; can you elaborate?

The above is a common idiom used to perform "one-shot" initialisation
the first time that a function is called.

--
Glynn Clements <glynn@gclements.plus.com>

Brad wrote:
> In G_gettext (lib/gis/locale.c), I ran across this code:
>
> char *
> G_gettext(const char *package, const char *msgid)
> {
> #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
> static char now_bound[4096];
> static int initialized;
>
> if (!initialized)
> {
> ...
> initialized = 1;
> }
> ...
> }
>
> Is there any particular reason that 'initialized' is not actually
> initialized? Is there a reason for this?

Glynn:

I don't understand what you are asking; can you elaborate?

The above is a common idiom used to perform "one-shot" initialisation
the first time that a function is called.

I think what Brad is getting at is that the "initialized" variable is
being used uninitialized in the if().

- static int initialized;
+ static int initialized = 0;

there is humor in that.

Hamish

On Fri, 2006-11-17 at 12:35 +1300, Hamish wrote:

> Brad wrote:
> > In G_gettext (lib/gis/locale.c), I ran across this code:
> >
> > char *
> > G_gettext(const char *package, const char *msgid)
> > {
> > #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
> > static char now_bound[4096];
> > static int initialized;
> >
> > if (!initialized)
> > {
> > ...
> > initialized = 1;
> > }
> > ...
> > }
> >
> > Is there any particular reason that 'initialized' is not actually
> > initialized? Is there a reason for this?

Glynn:
> I don't understand what you are asking; can you elaborate?
>
> The above is a common idiom used to perform "one-shot" initialisation
> the first time that a function is called.

I think what Brad is getting at is that the "initialized" variable is
being used uninitialized in the if().

- static int initialized;
+ static int initialized = 0;

Correct.

there is humor in that.

:slight_smile:

--
Brad Douglas <rez touchofmadness com> KB8UYR/6
Address: 37.493,-121.924 / WGS84 National Map Corps #TNMC-3785

Hamish wrote:

> > In G_gettext (lib/gis/locale.c), I ran across this code:
> >
> > char *
> > G_gettext(const char *package, const char *msgid)
> > {
> > #if defined(HAVE_LIBINTL_H) && defined(USE_NLS)
> > static char now_bound[4096];
> > static int initialized;
> >
> > if (!initialized)
> > {
> > ...
> > initialized = 1;
> > }
> > ...
> > }
> >
> > Is there any particular reason that 'initialized' is not actually
> > initialized? Is there a reason for this?

Glynn:
> I don't understand what you are asking; can you elaborate?
>
> The above is a common idiom used to perform "one-shot" initialisation
> the first time that a function is called.

I think what Brad is getting at is that the "initialized" variable is
being used uninitialized in the if().

- static int initialized;
+ static int initialized = 0;

Any static[1] variable without an explicit initialiser is
automatically initialised to "zero" (i.e. 0 for integers, 0.0 for FP,
NULL for pointers, arrays having each element initialised to zero and
structures having each field initialised to zero).

[1] By "static", I mean all top-level variables, plus any local
variables with the "static" specifier. Top-level variables are always
static (in the conventional sense) regardless of whether or not they
have the "static" specifier (which, for top-level variables, simply
means that they aren't exported from the object file).

The only difference between:

  static int initialized;
and:
  static int initialized = 0;

is that the former will typically result in the variable being stored
in the BSS segment while the latter will result in it being stored in
the data segment.

Because the BSS segment consists entirely of variables which are
initialised to zero, its contents don't actually need to be stored,
reducing the size of the executable (library, object file, etc).

[If you run "objdump -h" on any executable or shared library, you will
note that the BSS segment only has the ALLOC flag, as opposed to
CONTENTS, ALLOC, LOAD, DATA for the data segment.]

Short version: you only need an explicit initialiser for non-zero
values or for non-static local variables (which are allocated on the
stack at run-time, and have to be explicitly initialised upon entry).

--
Glynn Clements <glynn@gclements.plus.com>