static local variables vs global variables 
Author Message
 static local variables vs global variables

I would like to know the advantage of using a static local variable
(function scope ) versus global variable (since static local variables
are usually stored in the same region of memory used to hold global
variables. )

gopal



Sat, 24 Sep 2005 02:26:34 GMT  
 static local variables vs global variables

Quote:

> I would like to know the advantage of using a static local variable
> (function scope ) versus global variable (since static local variables
> are usually stored in the same region of memory used to hold global
> variables. )

> gopal

Well, for one thing, static local variables don't have external linkage,
so they can't be used by other translation units (think errno).  

The C++ part of me is also telling me something about the
time-of-initialization being unspecified for globals, but well-defined
for static locals.  That may all be bunk in C.

Ryan.



Sat, 24 Sep 2005 03:03:36 GMT  
 static local variables vs global variables

Quote:

> I would like to know the advantage of using a static local variable
> (function scope ) versus global variable (since static local variables
> are usually stored in the same region of memory used to hold global
> variables. )

The advantage is that the static local variable is not visible
outside the function it is declared in.  That way, other code
can't mess with it, either accidentally or intentionally.
--
"Given that computing power increases exponentially with time,
 algorithms with exponential or better O-notations
 are actually linear with a large constant."
--Mike Lee


Sat, 24 Sep 2005 03:09:45 GMT  
 static local variables vs global variables

Quote:


> > I would like to know the advantage of using a static local variable
> > (function scope ) versus global variable (since static local variables
> > are usually stored in the same region of memory used to hold global
> > variables. )

> > gopal

> Well, for one thing, static local variables don't have external linkage,
> so they can't be used by other translation units (think errno).  

static file scope variables also don't have external linkage.
Also, errno is not necessarily an identifier with external
linkage, but it is definitely a macro.  So, it is a poor example
of something with external linkage.

Quote:
> The C++ part of me is also telling me something about the
> time-of-initialization being unspecified for globals, but well-defined
> for static locals.  That may all be bunk in C.

It is all bunk in C.
--
"In My Egotistical Opinion, most people's C programs should be indented six
 feet downward and covered with dirt." -- Blair P. Houghton


Sat, 24 Sep 2005 03:11:59 GMT  
 static local variables vs global variables

Quote:



> > > I would like to know the advantage of using a
> > > static local variable
> > > (function scope ) versus global variable (since
> > > static local variables
> > > are usually stored in the same region of memory
> > > used to hold global
> > > variables. )

> > Well, for one thing, static local variables don't
> > have external linkage,
> > so they can't be used by other translation units
> > (think errno).

> static file scope variables also don't have
> external linkage.

Where did OP ask about static file scope variables?  I read his question
as asking about ordinary file scope variables, which do have external
linkage.  Perhaps the misunderstanding is mine.

Quote:
> Also, errno is not necessarily an identifier with
> external linkage, but it is definitely a macro.  
> So, it is a poor example
> of something with external linkage.

Well caught.  The standard (4.1.3 Errors) says that "it is unspecified
whether errno is a macro or an identifier declared with external
linkage".  Thanks for the correction.

Quote:
> > The C++ part of me is also telling me something about > > the time-of-initialization being unspecified for
> > globals, but well-defined
> > for static locals.  That may all be bunk in C.

> It is all bunk in C.

Darn it.  I remember reading a post a long while back in which someone,
possibly Dan Pop, showed a program consisting of three translation
units, whose output depended entirely on the link order.  I'm reasonably
sure it had to do with globals, too.  I've been poring through google
looking for it, but I can't find it.  Maybe somebody knows what I'm
remembering.

Thanks,
Ryan.



Sat, 24 Sep 2005 03:46:08 GMT  
 static local variables vs global variables

Quote:




> > > > I would like to know the advantage of using a
> > > > static local variable
> > > > (function scope ) versus global variable (since
> > > > static local variables
> > > > are usually stored in the same region of memory
> > > > used to hold global
> > > > variables. )

> > > Well, for one thing, static local variables don't
> > > have external linkage,
> > > so they can't be used by other translation units
> > > (think errno).

> > static file scope variables also don't have
> > external linkage.

> Where did OP ask about static file scope variables?  I read his question
> as asking about ordinary file scope variables, which do have external
> linkage.  Perhaps the misunderstanding is mine.

It depends on where you read the grouping.  He was either asking
about (static local) versus (global) variables or about static
(local versus global) variables.  It is hard to tell.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz


Sat, 24 Sep 2005 05:20:58 GMT  
 static local variables vs global variables

in comp.lang.c:

Quote:

> > I would like to know the advantage of using a static local variable
> > (function scope ) versus global variable (since static local variables
> > are usually stored in the same region of memory used to hold global
> > variables. )

> The advantage is that the static local variable is not visible
> outside the function it is declared in.  That way, other code
> can't mess with it, either accidentally or intentionally.

Balderdash!  Bovine excrement!  Ben, you know better.

A static object at function scope may or may not be visible outside
the function it was defined in.  It's identifier is most certainly
not, but there is a distinction between identifiers and the things
they name.

void silly_function(void)
{
   static int x;
   mess_with_my_static_local_variable(&x);

Quote:
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


Sat, 24 Sep 2005 10:16:16 GMT  
 static local variables vs global variables

Quote:


> in comp.lang.c:


> > > I would like to know the advantage of using a static local variable
> > > (function scope ) versus global variable (since static local variables
> > > are usually stored in the same region of memory used to hold global
> > > variables. )

> > The advantage is that the static local variable is not visible
> > outside the function it is declared in.  That way, other code
> > can't mess with it, either accidentally or intentionally.

> Balderdash!  Bovine excrement!  Ben, you know better.

> A static object at function scope may or may not be visible outside
> the function it was defined in.  

No, it's not visible outside the function it is defined in,
because that's outside its scope.  Here is the definition of
"visible":

     6.2 Concepts
     6.2.1 Scopes of identifiers
...
     For each different entity that an identifier designates, the
     identifier is visible (i.e., can be used) only within a
     region of program text called its scope.

I figured that everyone, especially c.l.c regulars, would know
this definition.

Quote:
> It's identifier is most certainly not, but there is a
> distinction between identifiers and the things they name.

> void silly_function(void)
> {
>    static int x;
>    mess_with_my_static_local_variable(&x);
> }

Well, sure, if you intentionally export its address, then it can
be used through that address.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}



Sat, 24 Sep 2005 10:48:07 GMT  
 static local variables vs global variables

in comp.lang.c:

Quote:


> > in comp.lang.c:


> > > > I would like to know the advantage of using a static local variable
> > > > (function scope ) versus global variable (since static local variables
> > > > are usually stored in the same region of memory used to hold global
> > > > variables. )

> > > The advantage is that the static local variable is not visible
> > > outside the function it is declared in.  That way, other code
> > > can't mess with it, either accidentally or intentionally.

> > Balderdash!  Bovine excrement!  Ben, you know better.

> > A static object at function scope may or may not be visible outside
> > the function it was defined in.  

> No, it's not visible outside the function it is defined in,
> because that's outside its scope.  Here is the definition of
> "visible":

>      6.2 Concepts
>      6.2.1 Scopes of identifiers
> ...
>      For each different entity that an identifier designates, the
>      identifier is visible (i.e., can be used) only within a
>      region of program text called its scope.

> I figured that everyone, especially c.l.c regulars, would know
> this definition.

I figured that especially you would recognize what I was saying.  My
quotation below is based on the definition above.  I was making a
distinction between the scope of the identifier used to define a
static (or indeed automatic) object at block scope, and visibility of
the object so defined.

Quote:
> > It's identifier is most certainly not, but there is a
> > distinction between identifiers and the things they name.

> > void silly_function(void)
> > {
> >    static int x;
> >    mess_with_my_static_local_variable(&x);
> > }

> Well, sure, if you intentionally export its address, then it can
> be used through that address.

To be very pedantic, there is no such thing as visibility of an object
according to the standard.  The only context in which the standard
uses visibility and semantically related words is in the context of
identifiers and type definitions.

Objects do not have visibility, but "can be used", and static linkage
or no linkage does not prevent them from being used outside the
defining block.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sat, 24 Sep 2005 11:30:28 GMT  
 static local variables vs global variables

Quote:
>Also, errno is not necessarily an identifier with external
>linkage, but it is definitely a macro.

          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The standard is not as sure as you:

    It is unspecified whether errno is a macro or an identifier declared
    with external linkage.

Quote:
>So, it is a poor example of something with external linkage.

I don't think so: no matter what it is, it behaves as an identifier with
external linkage.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 24 Sep 2005 20:59:54 GMT  
 static local variables vs global variables

Quote:
>I would like to know the advantage of using a static local variable
>(function scope ) versus global variable (since static local variables
>are usually stored in the same region of memory used to hold global
>variables. )

It doesn't matter where they are stored.  You choose one or another based
on what you actually need.  If you need a variable that keeps its last
stored value between function calls, that is not shared with any other
function, you define it at function scope.

"Global" variables are *always* used for sharing information between
different functions.  If all the functions using a "global" are in the
same source file, it's better to define the "global" with file scope and
internal linkage (i.e. using static, but outside of any function).  This
way, you're sure to avoid any conflicts with other "globals" with the same
name, defined in other files.

If needed, you can "export" such variables to functions from other files,
by passing them their address, but it's far better to design your programs
in such a way that this is not necessary.  Debugging a program whose
variables can change their values behind your back is not a particularly
pleasant experience...

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sat, 24 Sep 2005 21:17:18 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Watching global vs. local variable w/ same name

2. Static VS Local Class Level variables and Methods

3. Global variable or not global variable

4. Global structures and local variables problem.

5. About Global and Local Variables

6. Global and Local Variables with Same Name?

7. Same Variable Name: One Global, One Local

8. About local variables and variable size???

9. Initialisation of local static variables

10. Non-return of static local malloc'd variable

11. static local variables

12. local static variables under Windows XP (even more info)

 

 
Powered by phpBB® Forum Software