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

Where is my const char array stored?

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



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

Where is my const char array stored?
PostPosted: Tue Oct 07, 2014 2:05 am     Reply with quote

Device: PIC 24EP.
Compiler: v5.025.

Where is a const character array defined as
Code:

const char title[21]="Title 123:";

stored? RAM or ROM?

I have
Code:
#DEVICE PASS_STRINGS=IN_RAM

enabled and
Code:
#DEVICE CONST=ROM
disabled.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Oct 07, 2014 8:52 am     Reply with quote

It'll still be in ROM. But allow pointers as if it is in RAM.

The pass_strings=in_ram, means that the compiler will 'virtualise' constants, using a temporary RAM buffer to transfer them as used. Only wanted if you are putting the constants in ROM. Otherwise does nothing.

CONST=ROM, is the default. Will still be selected, unless you use CONST=READ_ONLY....

Remember either way, ROM has to hold it when the chip boots. If you use 'READ_ONLY', it is all copied to RAM at boot, and is then a RAM variable. Supposedly doesn't allow writing, but since the chip has no protection to stop this, you can easily write to it using a pointer.

The default changes to CONST=READ_ONLY, if you have ANSI selected.

Why have a const variable declared larger than the data it holds?. Just wastes space.
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Tue Oct 07, 2014 8:58 am     Reply with quote

One interesting thing to note is that if I put #DEVICE CONST=ROM in the header file.
The compiler starts giving compilation errors like "Expression must evaluate to a constant: on lines such as:
Code:

const int SIZE = 100;
char buffer[SIZE]="null";


I was just experimenting, displaying the text on 20x4 LCD, that's why the variable is of length 21.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue Oct 07, 2014 11:03 am     Reply with quote

Not surprised at that.
Generally CCS wouldn't allow a variable to be used at compile time like this till recently. The code that allows this to be done, only works with values held in RAM (which it now simulates at compile time), but to use a const requires a ROM value to be simulated in RAM. Normally you'd use a #define instead.
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Wed Oct 08, 2014 4:11 am     Reply with quote

Sorry Ttelmah, but I did not understand...
My question was that if CCS by default stores the constants in ROM then why does including #DEVICE CONST=ROM cause compilation error.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Oct 08, 2014 5:04 am     Reply with quote

A variable does not exist at compile time.
It only exists when the code is running.
So defining an array with a variable as a 'size', is a problem.
Historically CCS did not allow this.
However they now 'simulate' variables stored _in RAM_ to contain a value they are initialised with, so you can use a defined and initialised variable as a size.
Problem is that this simulation does not extend to variables stored and thought of as in ROM....

It is this last point that creates the problem, the simulation thinks the value is a variable.
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