View previous topic :: View next topic |
Author |
Message |
haxan7
Joined: 27 Jul 2013 Posts: 79
|
Where is my const char array stored? |
Posted: Tue Oct 07, 2014 2:05 am |
|
|
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 disabled. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19524
|
|
Posted: Tue Oct 07, 2014 8:52 am |
|
|
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
|
|
Posted: Tue Oct 07, 2014 8:58 am |
|
|
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: 19524
|
|
Posted: Tue Oct 07, 2014 11:03 am |
|
|
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
|
|
Posted: Wed Oct 08, 2014 4:11 am |
|
|
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: 19524
|
|
Posted: Wed Oct 08, 2014 5:04 am |
|
|
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. |
|
|
|