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

const and extern

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



Joined: 17 Jun 2019
Posts: 449
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

const and extern
PostPosted: Fri Feb 26, 2021 1:39 pm     Reply with quote

I recently ran in to a bug where a memcpy() was doing things in the wrong order, such as:

Code:

memcpy (&defaultData, myData, sizeof(myData));


It looks like the code's intent was to copy default values over into a working structure, but the destination/source was reversed so it was overwriting the defaults.

Using the const keyword can make compilers catch such accidents, but I get a linker error when trying to extern the const in a header file, such as:

Code:

HEADER FILE:

typedef struct
{
     int a,b,c;
} MyStruct;
...

extern const MyStruct defaultData;


SOURCE FILE:

const MyStruct defaultData=
{
   1, 2, 3
};


But I get a linker error on the CCS compiler, in my case complaining about "const1464" (did not appear anywhere in the map/list file). I feel like I've been down this path before, since I found I already had a C test program called ExternConst where I was doing this on a PC using GCC.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
Ttelmah



Joined: 11 Mar 2010
Posts: 16935

View user's profile Send private message

PostPosted: Sat Feb 27, 2021 2:31 am     Reply with quote

Thing is that const in CCS has a fundamentally different meaning to
const in GCC.

Const in ANSI C implies a standard variable that is designed so it should
not allow writes to it. So defining as const still has you using what is a RAM
based variable.
Const in CCS implies a 'variable' stored in the program memory, which in the
PIC is a different memory space to the RAM.

I'd have expected what you want to work, provided you select ANSI mode, or
CONST=READ_ONLY which should switch const to behaving like the ANSI
form.
jeremiah



Joined: 20 Jul 2010
Posts: 1203

View user's profile Send private message

PostPosted: Sat Feb 27, 2021 5:39 am     Reply with quote

And if you need "const" for actual constant data somewhere else, you can alternatively use the _readonly keyword as a replacement for the const you are normally used to.
temtronic



Joined: 01 Jul 2010
Posts: 7975
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 27, 2021 10:56 am     Reply with quote

I've always wondered how much 'overhead' is involved in making any variable like RAM or EEPROM 'read only' aka 'constant' ?
Ttelmah



Joined: 11 Mar 2010
Posts: 16935

View user's profile Send private message

PostPosted: Sun Feb 28, 2021 1:49 am     Reply with quote

I think it depends enormously on what chip is involved.
On most compilers, the 'const' declaration only affects the compile, not
the running code. It simply sets a flag so that if at any point in the code,
a call is made that potentially writes to the 'const', it raises an error for this.
Most don't/can't go further than this.
You can actually do things like pointer accesses and change a const,
on a lot of compilers.
Now a few chips do have hardware that can physically protect areas of
memory, and in some cases the const declaration instead uses this. This
does impose in some cases a lot of overhead.
Now the different CCS usage, is not a case of CCS being awkward. They used
const for the ROM, long before ANSI C used it. It is perhaps a pity that
the ANSI committee didn't elect to use a different keyword, like 'protected'
(which would better describe what const involves in this language), or
'unwritable'.
In the CCS 'const' use, the overhead depends on the chip version. On
the PIC16's, the program memory, only supports 14bit, so storing values
as const, adds extra maths to calculate where you are actually talking to,
and performing either RETLW instructions (which is a very efficient way
of retrieving simple values on these - on the early chips, the only way
of doing this), or the program memory read operation (which involves
several instructions more than a simple RAM read). On the PIC18's things
get easier. Unfortunately things get harder again on the PIC24/30/33,
though using PSV gets rid of this overhead (though there is a timing cost),
but restricts how much data can be stored, and uses more ROM space.
allenhuffman



Joined: 17 Jun 2019
Posts: 449
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 02, 2021 10:11 am     Reply with quote

Ttelmah wrote:

On most compilers, the 'const' declaration only affects the compile, not
the running code. It simply sets a flag so that if at any point in the code,
a call is made that potentially writes to the 'const', it raises an error for this.


This is exactly what I wanted this for. To catch an attempt to memcpy to data that was supposed to be the source. Smile
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
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