Re: To use global variables or no????
- From: John Devereux <jdREMOVE@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 06 Jun 2006 23:50:01 +0100
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
.
- References:
- To use global variables or no????
- From: eoindeb
- Re: To use global variables or no????
- From: Richard Heathfield
- Re: To use global variables or no????
- From: jaysome
- Re: To use global variables or no????
- From: Richard Heathfield
- Re: To use global variables or no????
- From: John Devereux
- Re: To use global variables or no????
- From: Richard Heathfield
- To use global variables or no????
- Prev by Date: Re: To use global variables or no????
- Next by Date: Re: a variable use of variables is what i'm after i think
- Previous by thread: Re: To use global variables or no????
- Next by thread: Re: To use global variables or no????
- Index(es):
Relevant Pages
|