View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
Simple question on Ram use |
Posted: Wed Oct 06, 2010 2:41 pm |
|
|
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
|
|
Posted: Wed Oct 06, 2010 3:13 pm |
|
|
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
|
|
Posted: Wed Oct 06, 2010 3:18 pm |
|
|
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
|
|
Posted: Wed Oct 06, 2010 4:46 pm |
|
|
Thank you both for your answers...
I appreciate it.
Gabriel. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|