Uninitialized static int counters?

Jarod Wilson jarod at wilsonet.com
Fri Feb 6 14:32:41 EST 2009


On Fri, 2009-02-06 at 13:57 -0500, Michael ODonnell wrote:
> OK - I'm seeing stuff like this the following in some kernel
> syscall handling code and it's making my brain hurt, so I hope
> somebody can explain it:
[...]
> WTF ?!?! #=->>          static int count;
> WTF ?!?! #=->>          if (++count <= 20)
[...]
> ...which, as far as I can tell, should yield effectively random behavior,
> yes?  Depending on the initial value of count we'll print the error
> message some number of times (once per pass through that routine) until
> count is incremented to a value greater than 19, after which we'll be
> silent until it wraps negative.  WTF?

Nope, in the kernel, all statics are initialized to zero unless you
provide an explicit initialization value. If you try to create a patch
that has 'static int foo = 0;' in it, and run it through checkpatch.pl
(included in the kernel source), it'll yell at you for doing so.

--jarod




More information about the gnhlug-discuss mailing list