473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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

Mar 3 '06 #1
9 2522
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.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 3 '06 #2
Richard Heathfield wrote:
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.

Mar 3 '06 #3
Joseph Wakeling wrote:
Richard Heathfield wrote:
File scope objects retain their values, whether they are
static-qualified or not.

3.1.2.4 Storage durations of objects

<snip... quote from C Standard>
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.
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.


--
BR, Vladimir

Avec!

Mar 3 '06 #4
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Joseph Wakeling wrote:
Richard Heathfield wrote:
File scope objects retain their values, whether they are
static-qualified or not.

3.1.2.4 Storage durations of objects


<snip... quote from C Standard>
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #5
Keith Thompson wrote:
.... 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/>
Mar 4 '06 #6
CBFalconer <cb********@yahoo.com> writes:
Keith Thompson wrote:
... 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.


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 4 '06 #7
Ico
CBFalconer <cb********@yahoo.com> wrote:
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.


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

--
:wq
^X^Cy^K^X^C^C^C^C
Mar 4 '06 #8
Ico wrote:

CBFalconer <cb********@yahoo.com> wrote:
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.


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/>
Mar 5 '06 #9
On Fri, 03 Mar 2006 08:14:21 -0800, Joseph Wakeling wrote:
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.

--
Ben.
Mar 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
7
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
6
by: dkeeney | last post by:
I have a newby type question on how global variables work between modules. I have a module test2.py that defines a global variable as well as two functions to operate on that variable. A script...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
13
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
11
by: dotnetnoob | last post by:
in the old VB, you can use global variables to hold commonly use data. i'll like to pass a variables selected by user in the combobox, how do you hold this variable for other object to use. does...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
6
by: pistacchio | last post by:
hi to all! can i load a module passing to it, automatically and as default, all the caller's global variables to act as module's global variables? thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.