This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

put const variables into code-segment

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

Parents
  • 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

Reply
  • 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

Children
No data