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

function input 'const' parameter

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



Joined: 12 Feb 2016
Posts: 11

View user's profile Send private message

function input 'const' parameter
PostPosted: Fri Nov 04, 2016 10:39 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 11:30 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 11:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 05, 2016 2:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 05, 2016 5:27 pm     Reply with quote

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.
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