View previous topic :: View next topic |
Author |
Message |
shson
Joined: 12 Feb 2016 Posts: 11
|
function input 'const' parameter |
Posted: Fri Nov 04, 2016 10:39 pm |
|
|
Hi
Does CCS support using const in the input parameter of a function like so?:
Code: |
#define UINT8 unsigned int8
UINT8 add(const UINT8 a, const UINT8 b) {
return a + b;
}
|
I'm using CCS v5.064.
Cheers
Eric |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 04, 2016 11:30 pm |
|
|
The standard for CCS is to put 'const' values in Flash memory ("ROM").
What would be the point of having fixed parameters in ROM for a function ?
The idea of a function is to have variable parameters (in RAM), so you
can call it with different input values. |
|
|
shson
Joined: 12 Feb 2016 Posts: 11
|
|
Posted: Fri Nov 04, 2016 11:45 pm |
|
|
PCM programmer wrote: | The standard for CCS is to put 'const' values in Flash memory ("ROM"). |
I see. It's a programming convention that I have used previously on other embedded platforms, mainly on Freescale MCUs and it was a standard practice for coding (although I was mainly on hardware design) from a company I used to work at. The idea was that functions that does not manipulate the input data, should not have a variable(s) that can be manipulated; cases like a fixed equation or mathematical calculation. They applied this 'style of coding' for desktop software written in C that interacted with the embedded system.
Thanks PCM!
Eric |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Sat Nov 05, 2016 2:35 am |
|
|
There is an important distinction here.
In most C's, 'const' is a RAM based variable that should not be changed. On chips that have memory protection, this is a powerful ability. However the PIC has no hardware for this, and CCS used the keyword 'const' before it was used in ANSI, for a 'variable' in ROM.
If you compile using the ANSI settings CCS switches to the value being in RAM (but with no actual protection).... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Sat Nov 05, 2016 5:27 pm |
|
|
You can also use the _readonly attribute:
Code: |
void func(_readonly int8 * test);
|
Note that in your example, using const on a non pointer type does not provide any benefit as they are passed by value and are merely copies anyways.
Side note: I sent a bug report to CCS that their _readonly and using #device CONST=READ_ONLY do not fully protect variables in all cases (they do in some and not others). CCS is aware and said they would have it fixed in an upcoming rev. |
|
|
|