>>>>> Glynn Clements <[EMAIL PROTECTED]> 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.
_______________________________________________
grass-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to