Revamped mail.

>-----Original Message-----
>From: Hugo van der Sanden

>I wanted a fast count of the number of set (clear) bits in a string.
>I'm getting rather strange results, and I haven't found a clue in
>the docs or archives. Here's the code:
>

Unfotunatly your code was scramble so it's difficult for me to help here
I think it would be great that each time one has a problem, the files needed
to reproduce the problem where mailled too so we could
extract/compile/parse/run and see what the problem is instead of having to
copy-past and guess what is not contained in the mail. This is general
remark not pointed specialy to you Hugo.

>Do I need to do something special to declare static variables?
No, it's plain C code.

>Or is this perhaps something you can't do at all? 
I wish we couldn't.

>(None of the examples in C-Cookbook, by the way, use a static variable.)
No because they strive to be good examples. I once wrote that the day one
has to eliminate a static from a module is the last day one uses static.

I don't know how much C you wrote in your life, so I don't want to sound
paternalistic just because you ask a beginner question. in short:
if you use a static variable in a function or a module:
- you can't use the function/module in multithreaded code
- you can't use the module without having full control over the call
sequence
- even if you have a single thread, you can't have two instances of your
object/module/structure (whatever it is) and guaranty it will work as
expected (even if you can have complete control you will be sharing your
module won't you?)
- your dog will get instantly momified and your mother in law will pay you a
visit every day (in fact she will move into your life)

I recently had to maintain a piece of code with 8 static variable for 500
lines of code. The first thing I did was to eliminate them. The code got
much clearer. To completely implement the requirements I had use 2 static
variables in a function (I'll do 200 push-ups and yes my mother in law is
coming back tomorow, my dog got momified 20 years ago). In my editor static
is white on red background and my coding standard is: "static int
static_variable_name = 0 ;". yes the variable name should contain static_
(as a punishment).

Add that un-initialised static are found in the bss segment and initialized
static are in the data segment.

Note that nothing of the above is valid for const static which are great.

Nadim


Reply via email to