Uninitialized static int counters?

Jerry Feldman gaf at blu.org
Fri Feb 6 14:44:32 EST 2009


On 02/06/2009 02:32 PM, Michael ODonnell wrote:
> ...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.
>   
True
> 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...
>   
There are rules that govern the compiler and the language. There are no 
special language constructs for the kernel, except that kernels are not 
compiled with libraries, so things like kprintf are compiled into the 
kernel. As I recall the rules for statics go all the way back to K&R. 
But, you are absolutely write about the wrapping. That should have been 
defined as an unsigned. But, I don't think you are going to see any 
Portugese messages :-)

BTW: The do { ... } while(0) will generate more code than { ... }. The 
compiler SHOULD easily optimize that out, and since this is really an 
error case, it does not matter that much even if the code is not 
optimized away.

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090206/0b2aade0/attachment-0001.bin 


More information about the gnhlug-discuss mailing list