473,513 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of static ?

Hello:

I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.

What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?

Thanks.

Nov 15 '05 #1
12 1889
In article <11**********************@z14g2000cwz.googlegroups .com>,
<co*******@gmail.com> wrote:
I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix. What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?


It isn't in global space in that example: the use of static places
it in file scope. If it were global then other routines could peek
at the data or possibly modify the data (const doesn't -promise-
read-only, it only -hints- read-only.)

--
Programming is what happens while you're busy making other plans.
Nov 15 '05 #2

Walter Roberson wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
<co*******@gmail.com> wrote:
I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.
What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?


It isn't in global space in that example: the use of static places
it in file scope. If it were global then other routines could peek
at the data or possibly modify the data


Makes sense. I wasn't thinking outside the box(outside his code as this
was not part of project). Thanks.

(const doesn't -promise-> read-only, it only -hints- read-only.)
Only if the compilers didn't complain about l-value error.

--
Programming is what happens while you're busy making other plans.


Nov 15 '05 #3

Walter Roberson wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
<co*******@gmail.com> wrote:
I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.
What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?


It isn't in global space in that example: the use of static places
it in file scope. If it were global then other routines could peek
at the data or possibly modify the data

makes sense. I wasn't thinking outside the box(outside this piece of
code) as their was no project involved. Thanks.

(const doesn't -promise-> read-only, it only -hints- read-only.)
Only if compilers didn't complain about l-value error.

--
Programming is what happens while you're busy making other plans.


Nov 15 '05 #4

<co*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello:

I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.

What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?


Of course [it's like a lot of things in C], static is used for a few things.

For things that would be 'global' (external linkage?) without using it,
static gives them file scope - that's the case here.

For local variables [within a function], static essentially gives the
variable the same storage *as though* it were declared outside of a
function, i.e., its value is retained for the life of the program. However,
it also restricts the variable's visibility - to that of the function in
which it was defined.


Nov 15 '05 #5
co*******@gmail.com wrote:
Walter Roberson wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
<co*******@gmail.com> wrote:
>I am trying to understand the use of static in this program.
>http://nanocrew.net/sw/nscdec.c for "inverse" matrix.

>What difference would it make if it were not static and just "const
>unsigned char inverse[ 128 ]" in global space which it already is ?


It isn't in global space in that example: the use of static places
it in file scope. If it were global then other routines could peek
at the data or possibly modify the data


Makes sense. I wasn't thinking outside the box(outside his code as this
was not part of project). Thanks.

(const doesn't -promise-> read-only, it only -hints- read-only.)
Only if the compilers didn't complain about l-value error.


That is not the main reason to declare it static. Is that inverse
matrix referenced in any other source file? If the answer is no, it is
good practice to declare it static to avoid polluting the global
namespace.

If it is not declared static and you have in an unrelated source file
(linked together with the same program) any other global object named
"inverse", (for example "int inverse = 0; /* if 1 simulation clock
runs backwards */ ) you will get multiple definition errors when
attempting to link them.
Nov 15 '05 #6
peetm wrote:
<co*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello:

I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.

What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?

Of course [it's like a lot of things in C], static is used for a few things.

For things that would be 'global' (external linkage?) without using it,
static gives them file scope - that's the case here.

For local variables [within a function], static essentially gives the
variable the same storage *as though* it were declared outside of a
function, i.e., its value is retained for the life of the program. However,
it also restricts the variable's visibility - to that of the function in
which it was defined.


No. An object defined in a function, static or not, is never visible
outside of the function.

The static qualifier is precisely two things in C. A storage class and a
linkage limiter. First, storage class. All objects defined outside of
any function (at file scope) have static storage class. That means that
the compiler allocates space for it and that it exsists for the life of
the program.

Other variables defined within functions default to automatic storage
class and cease to exist when the function returns. We can define an
object in a function with the 'static' qualifier. This gives the object
static storage class, meaning it is allocated by the compiler and lives
for the life of the program.

Because objects at file scope have static storage class by default, the
static keyword can take on another meaning. Also by default, objects and
functions defined at file scope enjoy external linkage, meaning they can
be 'seen' by the linker and therefore used by other modules in the
program. Now here at file scope, we can qualify an object as 'static'
and block its otherwise external linkage. The object or function is no
longer visible to the linker and therefore cannot be used by other modules.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #7
Joe Wright wrote:
[...]
Because objects at file scope have static storage class by default, the
static keyword can take on another meaning. Also by default, objects and
functions defined at file scope enjoy external linkage, meaning they can
be 'seen' by the linker and therefore used by other modules in the
program. Now here at file scope, we can qualify an object as 'static'
and block its otherwise external linkage. The object or function is no
longer visible to the linker and therefore cannot be used by other modules.


The object or function cannot be *directly* used by other modules.
However, it is still accessible in memory, and can be used by other
modules if a pointer to it is passed between modules.

--
Simon.
Nov 15 '05 #8
In article <11**********************@z14g2000cwz.googlegroups .com>,
co*******@gmail.com wrote:
Hello:

I am trying to understand the use of static in this program.
http://nanocrew.net/sw/nscdec.c for "inverse" matrix.

What difference would it make if it were not static and just "const
unsigned char inverse[ 128 ]" in global space which it already is ?


Do you think nobody else would ever have the idea to use an array named
"inverse" ?

Use of the "static" keyword means that this one file is the only place
where the "inverse" matrix is used. If you want to change the algorithm
used, and therefore change the size or contents of the "inverse" matrix,
you would have to check every single source code file whether it
accesses that array or not. Making it static means you have to check
only that one file.
Nov 15 '05 #9
Simon Biber wrote:
Joe Wright wrote:
[...]
Because objects at file scope have static storage class by default,
the static keyword can take on another meaning. Also by default,
objects and functions defined at file scope enjoy external linkage,
meaning they can be 'seen' by the linker and therefore used by other
modules in the program. Now here at file scope, we can qualify an
object as 'static' and block its otherwise external linkage. The
object or function is no longer visible to the linker and therefore
cannot be used by other modules.

The object or function cannot be *directly* used by other modules.
However, it is still accessible in memory, and can be used by other
modules if a pointer to it is passed between modules.


You misunderstand. The modules are compiled separately and then linked
together into an executable. There is no passing of pointers among
modules. Once linked, it's all one program. There are no modules anymore.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #10
On Sun, 16 Oct 2005 12:01:05 -0400, Joe Wright <jw*****@comcast.net>
wrote:
Simon Biber wrote:
Joe Wright wrote:
[...]
Because objects at file scope have static storage class by default,
the static keyword can take on another meaning. Also by default,
objects and functions defined at file scope enjoy external linkage,
meaning they can be 'seen' by the linker and therefore used by other
modules in the program. Now here at file scope, we can qualify an
object as 'static' and block its otherwise external linkage. The
object or function is no longer visible to the linker and therefore
cannot be used by other modules.

The object or function cannot be *directly* used by other modules.
However, it is still accessible in memory, and can be used by other
modules if a pointer to it is passed between modules.


You misunderstand. The modules are compiled separately and then linked
together into an executable. There is no passing of pointers among
modules. Once linked, it's all one program. There are no modules anymore.


If the modules are linked together, it is more than likely that
functions are being called from one module to another. It is also
likely that these function calls involve arguments and return values.
We will have to take your word for it that none of these arguments or
return values are of type pointer to something but that is not very
likely.
<<Remove the del for email>>
Nov 15 '05 #11
Barry Schwarz wrote:
On Sun, 16 Oct 2005 12:01:05 -0400, Joe Wright <jw*****@comcast.net>
wrote:

Simon Biber wrote:
Joe Wright wrote:
[...]
Because objects at file scope have static storage class by default,
the static keyword can take on another meaning. Also by default,
objects and functions defined at file scope enjoy external linkage,
meaning they can be 'seen' by the linker and therefore used by other
modules in the program. Now here at file scope, we can qualify an
object as 'static' and block its otherwise external linkage. The
object or function is no longer visible to the linker and therefore
cannot be used by other modules.
The object or function cannot be *directly* used by other modules.
However, it is still accessible in memory, and can be used by other
modules if a pointer to it is passed between modules.


You misunderstand. The modules are compiled separately and then linked
together into an executable. There is no passing of pointers among
modules. Once linked, it's all one program. There are no modules anymore.

If the modules are linked together, it is more than likely that
functions are being called from one module to another. It is also
likely that these function calls involve arguments and return values.
We will have to take your word for it that none of these arguments or
return values are of type pointer to something but that is not very
likely.


No. Once linked into an executable there are no modules. It is one
program and a function can call another directly without any regard for
which source or object module it was in originally. The functions may
indeed involve pointers and return values but they have nothing to do
with which module the were in or which module they were called from.
Once linked, there are no modules.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #12

In article <vs******************************@comcast.com>, Joe Wright <jw*****@comcast.net> writes:

You misunderstand. The modules are compiled separately and then linked
together into an executable. There is no passing of pointers among
modules. Once linked, it's all one program. There are no modules anymore.


This is true of some implementations, but not all. In EPM C on the
AS/400, for example, each translation unit becomes a separate "*PGM
object", which exists as the equivalent of a separate file in the
filesystem; and at runtime, when one *PGM object refers to a symbol
with external linkage in another *PGM object, that object is loaded
and dynamically bound to the running job (if it hasn't already been).
There's a link step, but it only serves to associate *PGM object
names with the external-linakge symbols they define.

In short, in this implementation, there are modules after program
creation. C does not require that a linker create a single "program"
entity which removes module boundaries.

--
Michael Wojcik mi************@microfocus.com

Art is our chief means of breaking bread with the dead ... but the social
and political history of Europe would be exactly the same if Dante and
Shakespeare and Mozart had never lived. -- W. H. Auden
Nov 15 '05 #13

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

Similar topics

8
4563
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
6556
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
1898
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
2088
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
2644
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
2546
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
6151
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
3510
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
5830
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
5980
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.