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

Unused variables

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



Joined: 06 Feb 2008
Posts: 26

View user's profile Send private message

Unused variables
PostPosted: Mon Feb 11, 2008 3:20 am     Reply with quote

Does anybody know how to avoid the unused variables from being mapped into memory?
Is there any level of optimization or another setting that can do this?
Of course, PCWH issues a warning, but the memory is still reserved.
I've checked it with PCH 4.068 (just downloaded) and 3.221.
And the question is NOT related to the "good programming warning-free style" Very Happy
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 7:04 am     Reply with quote

My first suggestion would be to remove all unused variables from your program but you will have figured that out yourself, so can you be a bit more specific as to why that's not possible?

One common 'unused variable' that I know of and is a pain in the *ss is the rs232_errors variable added by the compiler when you have the 'ERRORS' keyword in the rs232 configuration. The following trick was suggested by Bluetooth in this thread.

Code:
// A trick to avoid the compiler warning 'Variable never used:   rs232_errors'.
// This dummy function is never called and gets optimized away by the compiler.
// Funny, there is not something like an 'Unused function' compiler warning.
void never_called()
{
  rs232_errors = 0;
}
kolio



Joined: 06 Feb 2008
Posts: 26

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 8:22 am     Reply with quote

To be more specific:

1.There are two global variables declared:
volatile word StartTimer, StopTimer;

2.'StopTimer' is never referenced.

3.Output .ERR file:
...
>>> Warning 202 "source.c" Line 20(31,40): Variable never used: StopTimer
...

4.Output .SYM file:
...
1B7-1B8 StartTimer
1B9-1BA StopTimer
1BB MAIN.index
...

5.Output .STA file:
...
RAM used: 452 (29%) at main() level
479 (31%) worst case
...

6.If 'StopTimer' declaration is just deleted, then
output .STA file is:
...
RAM used: 450 (29%) at main() level
477 (31%) worst case
...

Is there any optimization level, where the unused variables will NOT eat up memory, but will be quietly or loudly (thru some warnings) discarded?

P.S. IMHO 'rs232_errors' has a good reason and shold be used instead of ignored. Handling framing and overrun errors is very important, but it's another story.
Ttelmah
Guest







PostPosted: Mon Feb 11, 2008 8:26 am     Reply with quote

It is also worth saying, that local variables that a not used, are not really 'mapped into memory'. CCS, is quite smart, and re-uses the memory space in subroutines, so though it may flag a variable as using a particular address, the same address will also be used in other routines above this. The warning, is a warning, not a terminal 'error', so just turning off this error number, may be all that is needed...

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 8:41 am     Reply with quote

What Ttelmah says is true for local variables but I don't think the memory is reused for global variables, at least it wasn't in the v3 compilers.

What I don't understand is why you want to have an unused variable in your source code? Doesn't sound like good programming practice to me, even in a library or something you would place it in conditional code (#ifdef).

Quote:
Is there any optimization level, where the unused variables will NOT eat up memory, but will be quietly or loudly (thru some warnings) discarded?
I don't know if this exists in the v4 compiler. I would like such a thing to exist for there is the CCS supplied library stdlib.h which has a global int32 _Randseed variable I never use this but it eats up valuable memory. Maybe we should start a new poll thread to convince CCS?
kolio



Joined: 06 Feb 2008
Posts: 26

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 9:05 am     Reply with quote

Yeah, the good old versions 2.xx...
ckielstra, you're damn right, buddy Laughing and you sniffed the problem.
There will be too much #ifdefs, because the product has 7 (yet) firmware profiles and 4 slight h/w differencies (assemble or not some components). One way is just to make for each one a separate set of files, but then any new common feature or fixed bug should be tracked at 7 projects. The other way is to have one common set of files (library) and 7 projects with the specific functionality. And here comes the #ifdef issue...

So, probably according CCS folks, removing unused variables is no longer an optimization problem, but "do-it-yourself" for Ver >= 3.xx.
Don't misunderstand me, I'm using CCS since 2000, last week just bought newest PCH and now little bit dissapointed by them.
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