Go to Post Given the time Banebots has already cost us, what's a little snow? - pfreivald [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

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-01-2007, 02:40
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 EricVanWyk View Post
I do love "as narrow as possible" scoping, but I prefer placing variables with their buddies.
If variables are "buddies", then group them in a struct. If the data needs to persist, then make the instance of the struct static. Functions that work on the data can then just take a pointer to the structure as an argument so one function can handle both left and right wheel data, for instance.

Write file modules around functions that work on one structure type. This allows you to mimic some of the good organization that comes with using C++ classes in C. File scope static variables are equivalent to class static variables. Static functions are equivalent to protected or private C++ functions. Only expose the public interface to struct with the global functions. This keeps things pretty encapsulated. You can preface all the global functions with a name related to the structure they work on to minimize name clashes.
  #2   Spotlight this post!  
Unread 25-01-2007, 21:09
prograid's Avatar
prograid prograid is offline
Registered User
AKA: Ben Cherian
FRC #0254 (The Cheesy Poofs)
Team Role: Alumni
 
Join Date: Oct 2004
Rookie Year: 2004
Location: San Jose
Posts: 80
prograid will become famous soon enough
Re: Why Static Variables?

Quote:
Originally Posted by EricVanWyk View Post
Wow, I just got pwnt. Very nice post.

Can you link us to more information on overlay variables? I am unfamiliar with them.
Thank you.
All the information I gave you was from the C18 Compiler manual, specifically page 20 of the User's Guide PDF on Kevin's site. (This is actually page 12 of the manual if you happen to have it printed out.)

One of the the key issues with overlay is that any sort of recursion is impossible. (e.g., you couldn't use it in the typical factorial implementation).
You will be given an error by the linker if this is the case though, so you shouldn't have to worry about random unknown errors popping up because of this.

The example you gave later in your post is correct (assuming I am inferring your intentions correctly). A static variable is used when you want the value of the variable to stay the same between function calls. An overlay or auto variable is used when you do not want that to be the case.

Quote:
Originally Posted by EricVanWyk View Post
The optimization I was referring to was the distinction between using the stack and not using the stack. Usually when one of my students proposed using a static variable, this was the purpose.
If that was the case, then they would not be optimizing the code, they would most likely be breaking it.


Quote:
Originally Posted by TimCraig View Post
If variables are "buddies", then group them in a struct. If the data needs to persist, then make the instance of the struct static. Functions that work on the data can then just take a pointer to the structure as an argument so one function can handle both left and right wheel data, for instance.
This is very good advice. Structs can help your code become much more organized. However, I personally prefer not using pointer arguments in robotics code, except for utility functions. If the structure needs to be used by more than one function make it a global or have it return a struct.
Closed Thread


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 04:05.

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