Hi. I want to put a (large) table of constant values into the code-segment, because I'm very low on RAM but have plenty of program-ROM. The C51-manual on page 88 says "Constant variables may also be stored in program memory". How is this possible? Thanks, Patrick
I haven't used the C51 compiler for a while but when I last did (V6.2) there was a directive 'code' that I used instead of const. This build tables in the code memory space. I hope this helps PhilipJ
const variables A constant variable ???? Anyhow 'const' has nothing to do with location DATA/IDATA/XDATA/CODE does Erik
"A constant variable ????" const int a=5;
that is a constant, not a variable. Erik
"The C51-manual on page 88 says 'Constant variables may also be stored in program memory'". Which edition of the manual do you have? In my 09.2001 edition, the above is on p90, and the very next paragraph says, "Program memory may be accessed using the code memory type specifier..." So this must be a case for RTWM - Read The Whole Manual! ;-)
"that is a constant, not a variable." not exactly!
const int five = 5; int x; x = 5; // Load the immediate constant 5 into x x = five; // Fetch the value of the variable five and load it into x
"that is a constant, not a variable." not exactly! Where does "five" vary (change)? It stays constant. OK, you address it as a variavble, but it is a constant. I realize that calling something by the oxymoron "constant variable" may have snuck into C litterature but it it still impossible to be "a constant AND a variable". Erik
"I realize that calling something by the oxymoron 'constant variable' may have snuck into C litterature but it it still impossible to be 'a constant AND a variable'." Probably the real error here is the use of the term "variable" - "identifier" would probably be better. Actually, to a 'C' compiler, const just means "non-writable." So, if you had a memory-mapped read-only port, you could mark it as const to make sure that any attempt to write it would be flagged as an error - although it could be anything but "constant" in the true English sense of the word! See also the use of const with the input parameters to functions; eg, http://www.keil.com/support/man/docs/c51/c51_strchr.htm
"that is a constant, not a variable" http://www.keil.com/support/docs/641.htm
Staffan: The fact that Keil use an oxymoron ("constant variable") does not make it correct. Erik
"The fact that Keil use an oxymoron ("constant variable") does not make it correct." I'm afraid you're wrong. In 'C' a const qualified type is not necessarily unmodifable. All that is required is that the variable is not modified as an lvalue. The compiler is allowed but not required to place a const qualified object in read-only storage, and is prevented from doing so if the volatile qualifier is always present. Consider: const volatile int xdata reg _at_ 0x100; Notice that no initialiser is present. The behaviour of the const keyword in C++ is different and seems closer to your expectations.
my reference states: .... So the declaration const int x5 = 100; declares x5 to be a constant integer(that is, it won't be set to anything else during the program's execution) My emphasis Erik
"const int x5 = 100; declares x5 to be a constant integer(that is, it won't be set to anything else during the program's execution)" Try this: int *ptr=&x5; *ptr=42; And this: extern const volatile int a; As for terminology: 5 //Integer constant #define X 5 //Integer constant int a; //Integer variable const int a; //Constant integer variable Compile this under 'C' and 'C++' to see the difference between a const in C++ (which is a constant) and a const in 'C': const int a;
I can take a knife and use it to peel an apple. I can use the same knife to kill someone. Knives are legal, does that mean that using it to kill someone is legal too? One of the most frequent objections to C is that it let you screw it up more than any other language (i.e. it does not stop you from duing unintended things). The ANSI C standard and K&R clearly intend that a const is read only. If it were not so, then why make it legal to locate it in ROM. It may be fun to find the "holes" in 'C' such as writing to const, I fail to se either the purpose or the value thereof. Erik
" The ANSI C standard and K&R clearly intend that a const is read only." Unless it is volatile. "It may be fun to find the "holes" in 'C' such as writing to const, I fail to se either the purpose or the value thereof." I don't wish to find holes in 'C'. I'm just trying to get you to understand that there is a difference between a constant and a const qualified variable.