Discussion in "General help Guidance and Discussion" started by    abbas1707    Jan 14, 2008.
Mon Jan 14 2008, 03:03 am
#1
hi,

i found two statements on keyword "const"...which confused me

1st


By identifying the variable as constant will cause compiler to store this variable in program memory rather than in RAM, thus saving space in RAM.


2nd


Many users bring up the idea of using C's keyword const as a means of declaring data to be in Program Space. Doing this would be an abuse of the intended meaning of the const keyword.

const is used to tell the compiler that the data is to be "read-only". It is used to help make it easier for the compiler to make certain transformations, or to help the compiler check for incorrect usage of those variables.

For example, the const keyword is commonly used in many functions as a modifier on the parameter type. This tells the compiler that the function will only use the parameter as read-only and will not modify the contents of the parameter variable.



In my opinion 1st is not correct..
what do u say??? please commenton it :-|
Mon Jan 14 2008, 09:26 am
#2
well considering the fact that we are talking abt embedded systems...
1. u just cant say 1 is false
2. 2 is absolutely right..
cause..

when u declare a variable constant CONST the compiler will normally arrange for const data items to be allocated in a memory region that the compiler calls .const.
so check ur linker files to know where u have this .const section
most compilers by default have it in programme memory :x
now that is not recommended :-|

Mon Jan 14 2008, 09:35 am
#3
i got it from the keil website

In ANSI C, the const type qualifier is used to define and access objects that are constant and that may not be changed. A variable that is declared with const may not be assigned to in the program.

The Cx51 Compiler conforms to the ANSI definition of const objects.

Variables declared with the const type qualifier alone are stored in the memory area (data, idata, xdata, and so on) associated with their definition.
Variables you want to locate in ROM must be declared with the code memory type. For example:
code char test[] = "This is a text string";
Variables declared with const far are stored in the HCONST memory area. This area is typically allocated to ROM memory.
Finally, the STRING compiler directive specifies where string constants are stored. This directive allows you to place strings in CODE memory, HCONST memory, or in XCONST memory (constant space in XDATA).
Constant objects are typically initialized when they are defined (in your source files). The following variable definitions show different ways to create constant objects:

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Richardgar
Sat Apr 20 2024, 11:05 am
AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am