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 CCS Technical Support

Simple question on Ram use

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



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Simple question on Ram use
PostPosted: Wed Oct 06, 2010 2:41 pm     Reply with quote

Hey all,

regarding Ram usage,

does returning a value from a function type int save any ram?

for example doing something like this:

Code:
VariableA= Function1(); // Function1 is type int
VariableB= Function2();// Function2 is type int

VariableC=VariableA+VariableB;


would the above save any ram compared to:

Code:
VariableA= Variable1;
VariableB= Variable2;
VariableC=VariableA+VariableB;


Provided that Var'1 and 2 are global and set by Func'1 and 2 respectively
and Func'1 and 2 are type VOID


also i was wondering if
i have several 1 bit (short) variable used a as a flags
does the compiler (assume 8 flags) put them all in the same byte?
even if they are not declared one after the other?
for example i have 4 flags in a driver.c and 4 more in another driver.c


Thanks in advanced

Gabriel
_________________
CCS PCM 5.078 & CCS PCH 5.093
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Oct 06, 2010 3:13 pm     Reply with quote

Many PICs have unused bits in some registers. The CCS compiler is smart enough to put the first few type short variables in those bits if they are available. So depending on your choice of PIC a few shorts may require no RAM at all. After that the compiler will group shorts compactly into bytes as needed whether they are declared as a group or not.

I don't really understand your first question. But whatever it is you can probably find out by compiling a couple of short test programs, one returning ints and the other using an alternative.
_________________
The search for better is endless. Instead simply find very good and get the job done.
ckielstra



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

View user's profile Send private message

PostPosted: Wed Oct 06, 2010 3:18 pm     Reply with quote

Global variables are using a fixed amount of RAM.
A variable returned from a function is placed on the stack and this space can be re-used. So yes, normally RAM is saved by returning variables instead of using global variables.

Questions like this can be tested yourself by compiling the application for both situations. Check the top of the generated list file (*.lst) for the amount of RAM used, and compare the results for both programs.

Regarding the 1-bit flags, here the compiler will try to combine multiple flags into one byte. However, there is no guarantee in how good the compiler will do this, one compiler version may do it better/worse than the next.
Again, you can test this yourself.
Have a look at the generated symbol file (*.sym) for the addresses used for your variables, when combined into a single byte you will see the flags use the same address.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Oct 06, 2010 4:46 pm     Reply with quote

Thank you both for your answers...

I appreciate it.

Gabriel.
_________________
CCS PCM 5.078 & CCS PCH 5.093
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