On May 7, 10:21 pm, rich...@[EMAIL PROTECTED]
(Richard Tobin) wrote:
> In article
<9c2b13d5-b18d-498e-aec8-d239d0bb3...@[EMAIL PROTECTED]
>,
>
> <vipps...@[EMAIL PROTECTED]
> wrote:
> >> It's a pity there isn't a standard way to get initialisation code
run.
> >I don't think it's that much of a problem. Well-designed code wouldn't
> >use global variables.
>
> You're letting a slogan override your common sense. There are many
> cases of global variables that are completely reasonable.
>
> For example, I want to convert between ISO Latin-5 (an 8-bit character
> set) and Unicode code points (which can be considered to have 2^16
> characters for this purpose). To do this, I have a table of 256
> entries mapping Latin-5 to Unicode, and I want to build the reverse
> table at start-up. These two global variables, latin_5_to_unicode and
> unicode_to_latin_5, have no objectionable properties.
Ah, indeed. Another example would be a table for all the is*()
functions in <ctype.h>, in a C lib implementation.
I just try to avoid global variables, because they can lead to a bad
design, when there's a better option available.
> > (with few exceptions such as errno)
>
> Not a reasonable example at all. It was adequate 20 years ago, but
> today is a fine example of the problems with global variables.
OK, you are right. I can see what you mean, one example would be with
threads I suppose.
I have not read much on alternative solutions to errno in C, so is
there a better solution?
Returning the error code is not possible (like pthreads) and an extra
parameter to every function that can fail would be quite annoying.


|