Uninitialized static int counters?

Michael ODonnell michael.odonnell at comcast.net
Fri Feb 6 14:32:13 EST 2009




> There is no such thing as Uninitialized static.  All static
> variables in C are initialialized by default according to the
> C standard.

> In the case of an int, it is initialized to 0.  In the code below,
> it is printing only the first 20 times mt_ioctl_trans() is called
> with an invalid command.

...and then it'll continue to increment and evenatually wrap
negative and we'll get brazilians of messages as (INT_MAX+20)
passes through that routine see that, indeed, count <= 20.

So, OK - this code is as b0rken as it appears.  I was worried
that it was depending on some {un,poorly}-documented "feature"
of Linux kernel programming that I wasn't aware of.

>What I see confufsing is:
>    do { ... } while(0);
> What this means is to go through the loop once.  You need a leading
> curly so you can set up counter as a local variable as variable
> names are block scope.  { ...  } would be equivalent to above.

Right - I get all that.  In fact I have always regarded
the slightly oddball do-while(0) construct as fairly useful,
particularly in macros where it can help you to guarantee you're
in your own little private block of code no matter what context
the macro is used inside of.  I was just concerned that the
rules for static ints had been bent in the Linux kernel code...



More information about the gnhlug-discuss mailing list