GlobalStatic 
Author Message
 GlobalStatic

Hi all,
        What you mean by global static variables in C?What are the advantages
and disadvantages??Any sample code will be appreciated.

Regds
Sobhan



Tue, 23 Nov 2004 12:49:52 GMT  
 GlobalStatic

Quote:

> Hi all,
>         What you mean by global static variables in C?

A global static variable? I guess it means "an object declared at file
scope, with the static storage class qualifier (otherwise it wouldn't be
static), in a translation unit which will not be linked with other
translation units" (otherwise it wouldn't be global).

Quote:
> What are the advantages
> and disadvantages??

The object in question is always there when you want it. That's an
advantage.
The object in question is always there when you don't want it. That's a
disadvantage.

Quote:
> Any sample code will be appreciated.

That's your job.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 23 Nov 2004 14:05:18 GMT  
 GlobalStatic

Quote:

> What you mean by global static variables in C?
> What are the advantages and disadvantages??

Someone has been imprecise at you.

By 'global' one usually means that a variable is
- at the top level of the file (i.e. not inside a '{}'), and
- shared between .c files (i.e. "global to the program"), i.e.
  *not* declared 'static'

The advantage of being at top level is that it can be used by several
functions.
The advantage of being 'global' with this meaning is that it can
be used by any function in any .c file in the program.

It's possible he meant that, and just meant by 'static' that such a
variable has what is called 'static storage duration' - i.e. that
storage is allocated at compile time and persists as long as the program
runs.

Or more likely, he meant that the variable is declared with the keyword
'static'.  This means the name is local to that .c file (and the headers
it #includes).  The advantage of that is that it won't clash with any
other top-level variables or functions with the same name in other files.

Quote:
> Any sample code will be appreciated.

static int counter = 0;

int count(void)
{
        return ++counter;

Quote:
}

--
Hallvard


Tue, 23 Nov 2004 14:58:34 GMT  
 GlobalStatic

Quote:

> > What you mean by global static variables in C?
> > What are the advantages and disadvantages??

> Someone has been imprecise at you.

> By 'global' one usually means that a variable is
> - at the top level of the file (i.e. not inside a '{}'), and
> - shared between .c files (i.e. "global to the program"), i.e.
>   *not* declared 'static'

Sobhan:I have written a piece of code:

 static OCTET
process information(OCTET*pFrame, OCTET *ie_ptr,
                              OCTET* crv_table)
{
    printf( "\n IE received \n") ;
    OCTET invoke_id;
//    OCTET crv;
//some thing

Quote:
}

When Iam writing it as the above it gives error as "undefined invoke_id" and
 while Iam writing as the below:
static process information(OCTET*pFrame,OCTET *ie_ptr,OCTET*crv_table
                            ,OCTET invoke_id)
It works with no error??Can you suggest any reason in it??

Quote:
> The advantage of being at top level is that it can be used by several
> functions.
> The advantage of being 'global' with this meaning is that it can
> be used by any function in any .c file in the program.

> It's possible he meant that, and just meant by 'static' that such a
> variable has what is called 'static storage duration' - i.e. that
> storage is allocated at compile time and persists as long as the program
> runs.

Sobhan:Do you mean you cannot call a static function from the main and
it will be compiled separately ???
Quote:
> Or more likely, he meant that the variable is declared with the keyword
> 'static'.  This means the name is local to that .c file (and the headers
> it #includes).  The advantage of that is that it won't clash with any
> other top-level variables or functions with the same name in other files.

> > Any sample code will be appreciated.

> static int counter = 0;

> int count(void)
> {
>    return ++counter;
> }



Tue, 23 Nov 2004 22:59:12 GMT  
 GlobalStatic

Quote:



> > > What you mean by global static variables in C?
> > > What are the advantages and disadvantages??

> > Someone has been imprecise at you.

> > By 'global' one usually means that a variable is
> > - at the top level of the file (i.e. not inside a '{}'), and
> > - shared between .c files (i.e. "global to the program"), i.e.
> >   *not* declared 'static'

> Sobhan:I have written a piece of code:

>  static OCTET
> process information(OCTET*pFrame, OCTET *ie_ptr,
>                               OCTET* crv_table)
> {
>     printf( "\n IE received \n") ;
>     OCTET invoke_id;
> //    OCTET crv;
> //some thing

Your code is unclear. OCTET is typedef'ed any place?  

- Show quoted text -

Quote:
> }

> When Iam writing it as the above it gives error as "undefined invoke_id" and
>  while Iam writing as the below:
> static process information(OCTET*pFrame,OCTET *ie_ptr,OCTET*crv_table
>                             ,OCTET invoke_id)
> It works with no error??Can you suggest any reason in it??

> > The advantage of being at top level is that it can be used by several
> > functions.
> > The advantage of being 'global' with this meaning is that it can
> > be used by any function in any .c file in the program.

> > It's possible he meant that, and just meant by 'static' that such a
> > variable has what is called 'static storage duration' - i.e. that
> > storage is allocated at compile time and persists as long as the program
> > runs.

> Sobhan:Do you mean you cannot call a static function from the main and
> it will be compiled separately ???

Take a moment. One thing is a static variable, and another so much
different a function declared as static.

- Show quoted text -

Quote:

> > Or more likely, he meant that the variable is declared with the keyword
> > 'static'.  This means the name is local to that .c file (and the headers
> > it #includes).  The advantage of that is that it won't clash with any
> > other top-level variables or functions with the same name in other files.

> > > Any sample code will be appreciated.

> > static int counter = 0;

> > int count(void)
> > {
> >       return ++counter;
> > }



Wed, 24 Nov 2004 03:06:03 GMT  
 GlobalStatic

Quote:




> > > > What you mean by global static variables in C?
> > > > What are the advantages and disadvantages??

> > > Someone has been imprecise at you.

> > > By 'global' one usually means that a variable is
> > > - at the top level of the file (i.e. not inside a '{}'), and
> > > - shared between .c files (i.e. "global to the program"), i.e.
> > >   *not* declared 'static'

> > Sobhan:I have written a piece of code:

> >  static OCTET
> > process information(OCTET*pFrame, OCTET *ie_ptr,
> >                               OCTET* crv_table)
> > {
> >     printf( "\n IE received \n") ;
> >     OCTET invoke_id;
> > //    OCTET crv;
> > //some thing

> Your code is unclear. OCTET is typedef'ed any place?

Sobhan:Yeah I have typedef the OCTET in the general.h file.

- Show quoted text -

Quote:

> > }

> > When Iam writing it as the above it gives error as "undefined invoke_id" and
> >  while Iam writing as the below:
> > static process information(OCTET*pFrame,OCTET *ie_ptr,OCTET*crv_table
> >                             ,OCTET invoke_id)
> > It works with no error??Can you suggest any reason in it??

> > > The advantage of being at top level is that it can be used by several
> > > functions.
> > > The advantage of being 'global' with this meaning is that it can
> > > be used by any function in any .c file in the program.

> > > It's possible he meant that, and just meant by 'static' that such a
> > > variable has what is called 'static storage duration' - i.e. that
> > > storage is allocated at compile time and persists as long as the program
> > > runs.

> > Sobhan:Do you mean you cannot call a static function from the main and
> > it will be compiled separately ???

> Take a moment. One thing is a static variable, and another so much
> different a function declared as static.

Sobhan:What I understood from the discussion in global static
variables
are  local to the file as the static variable is defined at the
beginning
of the file the values persists through out the file scope..The local
static variable is function scope..Is it right??

The other question is that:Is the static function is independentally
compiled
and we cannot call a static function from main..Is it right???

- Show quoted text -

Quote:
> > > Or more likely, he meant that the variable is declared with the keyword
> > > 'static'.  This means the name is local to that .c file (and the headers
> > > it #includes).  The advantage of that is that it won't clash with any
> > > other top-level variables or functions with the same name in other files.

> > > > Any sample code will be appreciated.

> > > static int counter = 0;

> > > int count(void)
> > > {
> > >  return ++counter;
> > > }



Wed, 24 Nov 2004 12:49:54 GMT  
 GlobalStatic

Quote:

>> Take a moment. One thing is a static variable, and another so much
>> different a function declared as static.

> Sobhan:What I understood from the discussion in global static variables
> are local to the file as the static variable is defined at the beginning
> of the file the values persists through out the file scope..
> The local
> static variable is function scope..Is it right??

Yes - and it too persists through the program's execution.  I.e. it is
shared between function invocation.

Quote:
> The other question is that:Is the static function is independentally
> compiled and we cannot call a static function from main..Is it right???

A 'static function' is a normal function whose name is only available
from the same file.  You can call it from main() if main() is in the
same file, or of course through a function pointer.

--
Hallvard



Fri, 26 Nov 2004 13:44:04 GMT  
 
 [ 7 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software