[GRASS-dev] is `initialized' initialized in HTML_Driver ()?

  Doesn't C require an explicit initial value for `initialized'
  here?

$ nl -ba grass-trunk-r33586/lib/htmldriver/Driver.c
...
    20 const struct driver *HTML_Driver(void)
    21 {
    22 static struct driver drv;
    23 static int initialized;
    24
    25 if (initialized)
    26 return &drv;
...
$

On Sun, 12 Oct 2008, Ivan Shmakov wrote:

  Doesn't C require an explicit initial value for `initialized'
  here?

Well, as it is a static variable it will be initialised to zero.

$ nl -ba grass-trunk-r33586/lib/htmldriver/Driver.c
...
   20 const struct driver *HTML_Driver(void)
   21 {
   22 static struct driver drv;
   23 static int initialized;
   24
   25 if (initialized)
   26 return &drv;
...
$
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Paul Kelly <paul-grass@stjohnspoint.co.uk> writes:

>> Doesn't C require an explicit initial value for `initialized' here?

> Well, as it is a static variable it will be initialised to zero.

  Oh, never knew C has such a feature. (Still, it may make sense
  to add an explicit initializer for the sake of clarity.)

[...]

Ivan Shmakov wrote:

>> Doesn't C require an explicit initial value for `initialized' here?

> Well, as it is a static variable it will be initialised to zero.

  Oh, never knew C has such a feature. (Still, it may make sense
  to add an explicit initializer for the sake of clarity.)

If you add an explicit initialiser, the variable will be placed in the
data segment.

Variables which are implicitly initialised to "zero" (i.e. any global
variables and "static" local variables lacking an explicit
initialiser) are placed in the BSS segment. As the entire BSS segment
is zero, its contents don't need to be stored in the resulting binary
file.

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

Glynn Clements <glynn@gclements.plus.com> writes:

>>> Doesn't C require an explicit initial value for `initialized' here?
>>> Well, as it is a static variable it will be initialised to zero.

>> Oh, never knew C has such a feature. (Still, it may make sense to
>> add an explicit initializer for the sake of clarity.)

> If you add an explicit initialiser, the variable will be placed in
> the data segment.

> Variables which are implicitly initialised to "zero" (i.e. any global
> variables and "static" local variables lacking an explicit
> initialiser) are placed in the BSS segment. As the entire BSS segment
> is zero, its contents don't need to be stored in the resulting binary
> file.

  Is this behavior mandated by some standard?

  It seems not a very smart decision to require such things. At
  least, gcc does, in my opinion, the right thing and puts the
  static variable initialized to zero (either explicitly or
  implicitly) to BSS:

$ diff -u foo[12].c
--- foo1.c 2008-10-12 22:54:02.281519397 +0700
+++ foo2.c 2008-10-13 22:51:21.117012403 +0700
@@ -1,7 +1,7 @@
int
main ()
{
- static int a;
+ static int a = 0;

   return a;
}
$ make CC=gcc foo1 foo2
gcc foo1.c -o foo1
gcc foo2.c -o foo2
$ nm foo1 | grep -F ' b '
00000000005007c4 b a.1609
00000000005007c0 b completed.5959
$ nm foo2 | grep -F ' b '
00000000005007c4 b a.1609
00000000005007c0 b completed.5959
$ diff -u <(gcc -o - -S foo{1,2}.c)
--- /proc/self/fd/63 2008-10-13 22:59:42.420439493 +0700
+++ /proc/self/fd/62 2008-10-13 22:59:42.401668748 +0700
@@ -1,4 +1,4 @@
- .file "foo1.c"
+ .file "foo2.c"
   .local a.1609
   .comm a.1609,4,4
   .text
$

  Of course, if there are the compilers that produce different
  results in these cases, it may make sense to leave the code in
  its present state.

Ivan Shmakov wrote:

>>> Doesn't C require an explicit initial value for `initialized' here?
>>> Well, as it is a static variable it will be initialised to zero.

>> Oh, never knew C has such a feature. (Still, it may make sense to
>> add an explicit initializer for the sake of clarity.)

> If you add an explicit initialiser, the variable will be placed in
> the data segment.

> Variables which are implicitly initialised to "zero" (i.e. any global
> variables and "static" local variables lacking an explicit
> initialiser) are placed in the BSS segment. As the entire BSS segment
> is zero, its contents don't need to be stored in the resulting binary
> file.

  Is this behavior mandated by some standard?

No.

  It seems not a very smart decision to require such things. At
  least, gcc does, in my opinion, the right thing and puts the
  static variable initialized to zero (either explicitly or
  implicitly) to BSS:

Hmm; so it does. It didn't always; back in 2.x it would always put
initialised variables in the data segment[1].

[1] This was quite important in the early days of the PlayStation, as
there wasn't any run-time loader. It just read the executable directly
into memory, so anything which ended up in the BSS segment would be
left uninitialised. They eventually figured out how to disable the
don't-store-the-contents-of-BSS behaviour, meaning that porting code
from the PC didn't begin with a day spent adding " = 0" everywhere.

  Of course, if there are the compilers that produce different
  results in these cases, it may make sense to leave the code in
  its present state.

I wouldn't recommend adding explicit initialisers everywhere just in
case people are unaware that they're not needed.

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