Patch: set value of static variables in cipher/random.c

Marcus Brinkmann marcus.brinkmann at ruhr-uni-bochum.de
Fri Jul 7 21:55:44 CEST 2006


At Fri, 07 Jul 2006 17:28:38 +0200,
Werner Koch wrote:
> 
> On Fri,  7 Jul 2006 17:10, haypo at inl.fr said:
> 
> > I'm not sure, but I think that a static variable have "undefined value".
> > So I don't understand how the code is working without value initialization
> 
> No they are not undefined.  They are initialized to zero. 
> 
> Your patch actually changes the code because the variables are moved
> from the BSS to the DATA segment.  That is in general not a problem
> but it increases the size of the binary.

Depends on the compiler, though.  Recent gcc's are smart enough to put
both in BSS, which means that the old rule "don't initialize static
variables to 0" is somewhat obsolete.  In newer code, explicitely
initializing for clarity may be preferred, if you want to.

Gcc's behaviour even makes sense if you don't initialize, because
sometimes initialization to 0 is hidden in macro abstractions.

Oh, and there is still a difference between "int foo = 0;" and "int
foo;".  The former allocates a variable in the BSS segment, but the
latter only defines a SHN_COMMON symbol, which is only allocated in
the last linking stage.  This allows to put "int foo;" in a header
file that is included by multiple files.  Doesn't mix well with
dynamic linking, though...

$ cat > main.c
int foo_noinit;
int foo_init = 0;

main () { printf ("%i %i\n", foo_noinit, foo_init); }
^D
$ make main > /dev/null 2>&1 | objdump --syms main | grep foo
080495a0 g     O .bss   00000004              foo_init
080495a4 g     O .bss   00000004              foo_noinit

Thanks,
Marcus




More information about the Gcrypt-devel mailing list