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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/16/09 18:25
Read: times


 
#165423 - Actually irrelevant to the const keyword
Responding to: ???'s previous message
Erik Malund said:
I don't see a problem with a compiler seeing the 'const' keyword for a global or static variable, and deciding to move the data into the code
in the '51 architecture different instructions are used to access 'const' and 'code'.

Erik

Correct, but irrelevant.

In the segmented x86 architecture, a different segment register was used to access data in a code segment, or in a data segment.

The 'const' keyword only means one thing: You may not change the data. A compiler is allowed to take advantage of this, by moving the data into memory regions that can't be written to. It is 100% up to the compiler - and it is up to the compiler to figure out what instructions/addressing modes that would take.

But a compiler may not make an equivalence between the 'const' keyword and code space. That would get the compiler - and hence the users - into real troubles when seeing the following code:
int a_function(const int a) {
    ...
    return something;
}

In the 8051 environment, the compiler need not require that you add any extra code or __code or whatever keyword when you create your global constant variables.
const char my_data[] = {...};
const __code char my_data[] = {...};
__code char my_data[] = {...};

The two later declarations would force the 'my_data' variable into the code. But a compiler might decide that any of the above declarations is enough to store my_data in the code segment. It may decide that __code implies const. It may decide that const for global or static data should force the data into the code region. But it may never make the decision that const is equivalent with __code, or it would fail on const-declared auto "variables".

List of 25 messages in thread
TopicAuthorDate
C lang. question            01/01/70 00:00      
   Confused            01/01/70 00:00      
      re:            01/01/70 00:00      
         Did you get the job?            01/01/70 00:00      
            Do you _want_ the job?            01/01/70 00:00      
               Same same            01/01/70 00:00      
                  Yes, that was exactly what I meant!            01/01/70 00:00      
            re:job            01/01/70 00:00      
            OR            01/01/70 00:00      
               I wouldn't have thought so?            01/01/70 00:00      
                  Who knows            01/01/70 00:00      
                     not really applicable            01/01/70 00:00      
                        I agree            01/01/70 00:00      
                           Still missing the point.            01/01/70 00:00      
                              using 'const' for 'code' would be very bad            01/01/70 00:00      
                              Two examples            01/01/70 00:00      
                                 Different issues            01/01/70 00:00      
                                    architectual            01/01/70 00:00      
                                       Actually irrelevant to the const keyword            01/01/70 00:00      
                                          if it is irrelevant, then why ...            01/01/70 00:00      
                                             Because they are not equivalent            01/01/70 00:00      
                                          Exactly.            01/01/70 00:00      
                                             Standard mechanisms for extensions            01/01/70 00:00      
      volatile applies to data - not functions            01/01/70 00:00      
   Delay loops in 'C' (or any other HLL)            01/01/70 00:00      

Back to Subject List