CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

efficient code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vtrx



Joined: 11 Oct 2017
Posts: 2

View user's profile Send private message

efficient code
PostPosted: Wed Oct 11, 2017 1:50 pm     Reply with quote

What is the best option for the variables I'm going to use?
I'm using the 18f4550 with USB.
Volatile, Static or none of them?
Example:
Code:
 
 int8 joy[4] ={0, 0, 0, 0};  //Static,Volatile?
 INT8 in_data[USB_EP1_RX_SIZE];
 INT8 KeyA[11] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
temtronic



Joined: 01 Jul 2010
Posts: 6066
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 11, 2017 4:43 pm     Reply with quote

The answer is a firm 'any of them'.....
The 'correct' answer should be based upon the entire program and PIC choice.
Some programmers prefer to use all 'global' variables which means each has it's unique RAM space. The potential problem is that with big programs or sloppy code, you can run out of RAM. A global variable can be accessed at any time from anywhere in the program.
To save on RAM, you use 'local' variables. Doing this, one RAM location is 'reused' for temporary storage of data in several functions. However the data contained within that RAM address is ONLY valid within that function. As soon as the program leaves that function, that RAM address can (will) be used by some other part of the program for some other data storage.

'static' I believe IS a global variable that is set to '0' (zero) at the beginning of the program. Remember that unless YOU specifically say 'x=22', then the data at RAM address of 'x' can be anything...

'efficient' code is always a trade off of speed versus size. Some code can be physically small yet consume a lot of time to execute. Other code can be huge yet be very,very fast. Both do the same task so it's up to the programmer to decide which to use.

jay
guy



Joined: 21 Oct 2005
Posts: 268

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 12, 2017 2:36 am     Reply with quote

The way I do it is - if the variable is only used inside a function, I use a local variable (simply declared inside the function). It then 'lives' only during the function execution.
If the variable (or its contents) is used in several functions or main(), I use a global variable (simply declared at the beginning of the code, usually in a separate #include file). To make it global simply declare it outside of a function.
In both cases I use a simple
Quote:
int8 newVar;
- no need for other reserved words.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 4:31 am     Reply with quote

Same here.

Unless you are using multiple compilation units, "static" outside of a function does nothing I believe.
jeremiah



Joined: 20 Jul 2010
Posts: 981

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 6:24 am     Reply with quote

static would force the compiler to initialize a variable to 0 if no initialization is given.

volatile is most often not needed in CCS. I've actually run into 2 instances where it was needed in the PCD compiler, but with over 10 years of working on PIC24 chips, that is a very small subset. generally you only need to use volatile on

1. hardware register mapped variables, but I believe CCS does this by default on variables mapped with #BYTE, #WORD, and #BIT anyways

2. Variables shared between ISR and main code. Again, here CCS doesn't normally optimize out variables. The two instances I ran into were where CCS opted to save a variable to a register and then loop on that register result instead of the actual variable, and then there was some sort of optimization on another code chunk where an entire variable ended up being ignored. Both were part of very large and complex code bases. Now I use volatile because it is good C form for those situations, but I know most others do not and rarely (never?) run into problems, so for CCS you are generally safe not using it.

Technically volatile can make code less efficient (since it forces the compiler to read/write directly to that memory space and not rely purely on registers). That's why you should only use it where needed.
guy



Joined: 21 Oct 2005
Posts: 268

View user's profile Send private message Visit poster's website

PostPosted: Thu Oct 12, 2017 10:52 am     Reply with quote

Quote:
"static" outside of a function does nothing I believe

If I'm not mistaken, declaring a variable as static inside a function reserves the space in memory and creates a 'global' variable for that function specifically. It could be used for example as a neat way to declare variables: if a function (or ISR) handles an incoming string and divides it into bits and bytes, the bit & byte counter are only useful inside that function. In such case they could be declared as static inside the function. They will (obviously) not be initialized each time the function is called.
Ttelmah



Joined: 11 Mar 2010
Posts: 13049

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 11:00 am     Reply with quote

A static variable inside a function, creates a variable that is permanently 'owns' a specific memory area, but has a 'name' that is meant to be local to the function (not global). If it is non initialised, it is set to zero at boot. If it is initialised, it is initialised once at boot, not each time the function is called.

In CCS, it is actually a global variable, but has the function name as a prequel to it's name. So if you declare a static called 'fred' inside a function called 'dick', the variable created is actually dick.fred
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group