Global variables in modules---preserving values between module calls

  • Thread starter Joseph Wakeling
  • Start date
J

Joseph Wakeling

Hello all,

I sometimes make use of global variables within modules, which I denote
with "static" so that they will not be seen outside the module, and
will preserve values between module calls.

Recently I was looking back at a rather important function and found
I'd forgotten to put the static declaration in place. I'm not worried
about any interference with outside---the variable names are unlikely
to be reproduced elsewhere---but I am *very* concerned that the values
stored in these variables should have been preserved between module
calls. Else it means I have to throw a lot of data away... :-(

Is this latter feature standard for globals in modules, or is the
"static" designation required here? Certainly the results of the code
would seem to suggest the values were preserved. However, it's not
clear to me that this wasn't just the compiler (gcc -O3 -ansi -pedantic
-Wall ...) being nice to me.

Many thanks,

-- Joe
 
R

Richard Heathfield

Joseph Wakeling said:
Recently I was looking back at a rather important function and found
I'd forgotten to put the static declaration in place. I'm not worried
about any interference with outside---the variable names are unlikely
to be reproduced elsewhere---but I am *very* concerned that the values
stored in these variables should have been preserved between module
calls. Else it means I have to throw a lot of data away... :-(

File scope objects retain their values, whether they are static-qualified or
not.

3.1.2.4 Storage durations of objects

An object has a storage duration that determines its lifetime.
There are two storage durations: static and automatic.

An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. For such
an object, storage is reserved and its stored value is initialized
only once, prior to program startup. The object exists and retains
its last-stored value throughout the execution of the entire
program.

(In your case, the object has external linkage.)
 
J

Joseph Wakeling

Richard said:
File scope objects retain their values, whether they are static-qualified or
not.

3.1.2.4 Storage durations of objects

An object has a storage duration that determines its lifetime.
There are two storage durations: static and automatic.

An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. For such
an object, storage is reserved and its stored value is initialized
only once, prior to program startup. The object exists and retains
its last-stored value throughout the execution of the entire
program.

(In your case, the object has external linkage.)

Great. Thanks very much for the clarification. What is the text that
you quote from?

The external linkage issue is another kettle of fish, or more properly,
can of worms... ;-) Thank God within my own small and fairly limited
code it's never been that much of an issue, and a code error like the
one I described usually has no negative side-effects.
 
V

Vladimir S. Oka

said:
Great. Thanks very much for the clarification. What is the text that
you quote from?

It's the ISO C Standard. You can buy a PDF of it (around 18 USD, I'm led
to believe), or download N869.pdf (and other formats) which has a very
close draft version, and is free. I don't have links handy, but they've
been mentioned in this group fairly recently. GIYF.
 
K

Keith Thompson

Vladimir S. Oka said:
It's the ISO C Standard. You can buy a PDF of it (around 18 USD, I'm led
to believe), or download N869.pdf (and other formats) which has a very
close draft version, and is free. I don't have links handy, but they've
been mentioned in this group fairly recently. GIYF.

Judging by the section number, it's the ANSI C standard of 1989, or a
draft of it. The 1990 ISO standard is functionally identical to the
1989 ANSI standard, but the sections were renumbered; ANSI's section 3
became ISO's section 6.

Officially, the current standard is the 1999 ISO standard (there's no
separate ANSI standard corresponding to this), but the new standard
isn't (yet?) widely implemented.

If you're interested in C99, I suggest n1124.pdf rather than n869.pdf.
n869 is a draft that preceded the approved standard; there were a few
significant changes. n1124 includes the approved C99 standard plus
some changes that have been made since then (TC1 and TC2); the changes
are marked with change bars.
 
C

CBFalconer

Keith said:
.... snip ...

Officially, the current standard is the 1999 ISO standard (there's no
separate ANSI standard corresponding to this), but the new standard
isn't (yet?) widely implemented.

If you're interested in C99, I suggest n1124.pdf rather than n869.pdf.
n869 is a draft that preceded the approved standard; there were a few
significant changes. n1124 includes the approved C99 standard plus
some changes that have been made since then (TC1 and TC2); the changes
are marked with change bars.

Except that N869 is available in a text version, and n1124 is not.
For most purposes N869 is quite adequate, and actually
manipulatable with grep etc.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

CBFalconer said:
Keith Thompson wrote:
... snip ...

Except that N869 is available in a text version, and n1124 is not.
For most purposes N869 is quite adequate, and actually
manipulatable with grep etc.

For those who don't mind PDF, n1124 is better than n869. (And if you
have the right tools, you should be able to generate plain text from
n1124.pdf.)
 
C

CBFalconer

Ico said:
A text-only version is available at http://zevv.nl/div/n1124.txt

Thanks. Unfortunately that needs a lot of work. N869.txt fits on
65 or so char wide pages, and is quite suitable for quote material
in newsgroups.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
B

Ben Bacarisse

Recently I was looking back at a rather important function and found I'd
forgotten to put the static declaration in place. I'm not worried about
any interference with outside---the variable names are unlikely to be
reproduced elsewhere---but I am *very* concerned that the values stored in
these variables should have been preserved between module calls.

You may well have you answer already, but since C does not have "module
calls" I wanted to check what you really mean. Take, for example:

static int some_data = 42;

int function(void)
{
static int some_other_data = 99;
...
}

In this case, you can "safely" (in your sense) leave off the first static,
but forgetting the second will alter the program's meaning significantly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top