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
"Even a volatile const (if that's allowed at all...)" It is. "object is still read-only --- for the C program." No. An interrupt service routine can modify that volatile const object. "What other elements of the system might do to it is beyond the scope of not just the 'const' keyword, but of the entire language standard." The draft C99 standard I have to hand gives the above as an example.
"An interrupt service routine can modify that volatile const object." What, so you mean the object is declared without const for the ISR, but with const for main() - thus allowing the ISR to write it, but main() to only read it? That sounds like a good idea - I shall have to bear it in mind.
What, so you mean the object is declared without const for the ISR, but with const for main() - thus allowing the ISR to write it, but main() to only read it? That sounds like a good idea - I shall have to bear it in mind. From what I recall from when const was introduced... The intention of const was to provide a means of protecting variables from modification outside the "write scope". In an ideal world, the variable would be local but would be passed to functions as a const argument (much like the string functions in the C library). However, I'm pretty sure the same can be done with global variables. Places that must modify the const variable must use some typecasting trickery to make things work (by either creating a pointer to the const or by typcasting the variable without the const qualifier). However, in reality, it's much easier to just use const on function arguments. This makes it clear to someone which arguments may be affected by looking only at the function prototype. Jon
Well, that's no argument, since a) interrupt service routines can't be written in the "C" that provides the applicable definition of these terms (i.e. the ISO C standard), and b) even if they could, there's no conforming way to get access to a const object that would allow the ISR to write to an object the main code can't. Whichever way you try (pointer casts, trick unions): you always cross an "undefined behaviour beyond this line".