Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Use of static ?

Reply
Thread Tools

Use of static ?

 
 
codefixer@gmail.com
Guest
Posts: n/a
 
      10-14-2005
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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      10-14-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
codefixer@gmail.com
Guest
Posts: n/a
 
      10-14-2005

Walter Roberson wrote:
> In article <(E-Mail Removed) .com>,
> <(E-Mail Removed)> 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.


 
Reply With Quote
 
codefixer@gmail.com
Guest
Posts: n/a
 
      10-14-2005

Walter Roberson wrote:
> In article <(E-Mail Removed) .com>,
> <(E-Mail Removed)> 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.


 
Reply With Quote
 
peetm
Guest
Posts: n/a
 
      10-15-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.




 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      10-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>Walter Roberson wrote:
>> In article <(E-Mail Removed) .com>,
>> <(E-Mail Removed)> 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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-15-2005
peetm wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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 ---
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      10-15-2005
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.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      10-15-2005
In article <(E-Mail Removed) .com>,
(E-Mail Removed) 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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-16-2005
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 ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
Static is REALLY Static! Paul W ASP .Net 2 05-03-2005 10:12 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM
Static vs. non-static connection Natan ASP .Net 8 05-26-2004 08:21 AM



Advertisments