Re: To use global variables or no????



Richard Heathfield <invalid@xxxxxxxxxxxxxxx> writes:

John Devereux said:

Richard Heathfield <invalid@xxxxxxxxxxxxxxx> writes:

I can think of two reasons off-hand for using non-auto non-const objects,
one of which I don't like but am prepared to accept, and the other of
which I don't like but don't have any choice about accepting.

I might be misunderstanding you, but don't some types of function need
to keep "static" state information? E.g. functions like malloc?

My apologies, John, for not making myself clear enough.

That's OK. I could see you were in riddle mode :)


I wasn't thinking about implementing the standard library, but about
more general programming tasks. It would be impossible, I think, to
write strtok in C without using a static duration object. And yes,
the mm subsystem needs to keep some state too. (Actually, strtok
/wouldn't/ need to keep state if they'd thought about it a bit more;
it appears to have been conceived in the days when "get it done and
never mind how as long as it's quick" was a view more widely held
than it is today.)

The two uses I had in mind are:

(a) you have a whole bunch of configuration information, which is needed by
just about every function in your shiny new library (for example, the
current default size for dynamic buffers, that sort of thing), and you
don't wish to impose upon your users the burden of passing a
pointer-to-config-info to absolutely every function in the library. That's
the use I don't like, but I can live with.

This is what I end up using them for. I have a global structure, say
"settings". Lots of functions need to be able to access the members,
in lots of different modules. Most functions only need access to a
couple of members each. But they need to be in a single big structure
so that menu and serialisation code can get at them conveniently. I
can pass around pointers to that structure, and have the structure
declared "static" somewhere at file scope instead of "extern". But I
am not really sure I gain anything by doing that!

I keep thinking there must be a better way but have not found one
yet...

--

John Devereux
.



Relevant Pages

  • Re: Never seems to change.........
    ... alter my Faith fails, which it has, then you have lost your time and ... advices of the House members, the Counsellors, Aux. ... This involves an open and inquiring mind. ... The truth is ...
    (talk.religion.bahai)
  • Re: Grammar Question
    ... Bambi C. ... your change of the sentence making "members" the subject would ... Family is singular, but has more than one mind, so "if the ... But neither does "if the family changes their minds" sound right. ...
    (sci.med.transcription)
  • Re: Is the empty set a number?
    ... container of a group and have no value on its own. ... an abstract idea that we can define a group without members. ... No i consider the empty set to be a construct of human mind to express ...
    (sci.math)
  • Re: Home Depot , Fox TV, and Global Warming
    ... people join a mass movement first and only then adopt the ... Members of a mass movement ... "People mind their own business when it's worth minding. ...
    (alt.home.repair)
  • Re: [C] Simple array question
    ... > of leaving error checking at that. ... What did you have in mind for ... Customised parsing routines, commonly based around strtok or (for those who ...
    (alt.comp.lang.learn.c-cpp)