Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/13/02 08:56
Read: times


 
#29110 - RE: Const in code, Andy

Let me explain this better: as of today, with the Raisonance compiler, if you write

const char msg[] = "ACME Wotsit v2.03";

the whole string goes both in code *and* in data (meaning that it is in code and is copied in data at startup). Now, having the string in data is a waste because you can't modify it anyway (unless you use some tricks, see below) because you have declared it as const (the compiler will produce an error, not just a warning)

From this point of view, if the compiler puts variables that can't be modified anyway in code, it saves space. It would also help some users who believe that the const qualifier is enough to put the variable in code, but, as Andy could surely remind us, that can be solved by reading the manuals.

However, Peter has brought to light an interesting issue: using const to protect a variable from being modified elsewhere than where it is intended to. Now, Peter, how do you modify your const variable in the interrupt routine (just an example)? I can see how to do that using some casts, but I'm interested in the way you do it (and with which toolchain).

All in all I think that we will just add a warning when a const declaration is found to tell the user that his variable is going to be duplicated in code and data and that he *might* want to use "code" instead of "const" to prevent this.

Regards,

Luca


List of 12 messages in thread
TopicAuthorDate
Define Vs Constant            01/01/70 00:00      
RE: Define Vs Constant            01/01/70 00:00      
RE: Define Vs Constant            01/01/70 00:00      
RE: Define Vs Constant (Andy)            01/01/70 00:00      
RE: Define Vs Constant Luca            01/01/70 00:00      
RE: Define Vs Constant (Andy)            01/01/70 00:00      
RE: Define Vs Constant (Luca            01/01/70 00:00      
RE: Const in code            01/01/70 00:00      
RE: Const in code, Andy            01/01/70 00:00      
RE: Const in code, Andy            01/01/70 00:00      
RE: Const in data, Luca            01/01/70 00:00      
RE: Const in code, Luca            01/01/70 00:00      

Back to Subject List