Go to Post Wow, almost seems unfair to have that many nice people in one place. Isn't it possible to overload on cool? - Molten [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Spotlight this post!  
Unread 23-01-2007, 23:50
TimCraig TimCraig is offline
Registered User
AKA: Tim Craig
no team
 
Join Date: Aug 2004
Rookie Year: 2003
Location: San Jose, CA
Posts: 221
TimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to behold
Re: Why Static Variables?

Quote:
Originally Posted by brianafischer View Post
I am trying to create a list of "good" programming practices for the students. I cannot seem to google a good reason for proper use of static variables instead of globals. Anyone interested in sharing some knowledge?

When is a static variable required over a global?
What are the advantages of static variables?
What are the disadvantages of static variables?

Thanks!
Two properties of variables in C are persistence and scope or visibility. Globals and statics are persistent since they hold their values for the duration of program execution as opposed to automatic variables which become invalid when the execution of the program exits the scope where the automatic is defined. Variables allocated on the heap persist between the malloc (or similar) and free calls.

Global variables have program scope meaning they can be seen anywhere in the entire program provided a declaration is visible. Static variables can have file, function, or even block scope. Automatic variables can only have function and block scope by contrast. The visibility of heap variables is controlled by the visibility of the pointer variables which, well, point to them.

Good programming practices usually suggest that variables be given the smallest scope to do the job. This prevents accidental access via things like mispelling the variable name. It also limits name clashes for commonly used variable names and makes the logic easier to follow. That said, most programmers don't use block scope in C and the smallest common scope is the function. Using more descriptive names helps with name clashes but it's nice to be able to use a simple descriptive name without having to worry too much about it until the linker complains. Some coding styles preface global variables with "g_" and file scope statics with "s_" to help with clashes and to make it more obvious what the variable really is.

With microcontrollers like the PIC which have limited variable space and especially limited stack space, it can be more efficient to use statics or grobals to more efficiently use memory. Also, from a speed standpoint, some microcontrollers can access static memory faster than a stack variable (I'm not sure about the PIC in this respect). Memory management with a microcontroller can take more understanding than for your typical PC program.
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Cling?? orelinde Electrical 17 27-02-2006 17:33
Static Variables amateurrobotguy Programming 8 04-03-2005 00:40
Static Electricity JeffO Championship Event 5 19-04-2004 11:04
Static on the HDPE Al Skierkiewicz Technical Discussion 60 12-03-2003 17:52
Static Electricity archiver 2001 12 24-06-2002 00:34


All times are GMT -5. The time now is 23:51.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi