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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/12/02 00:59
Read: times


 
#29041 - RE: Define Vs Constant
I'm in stream-of-conciousness mode here...

There is probably a lot of room for opinionating on this topic. I'll try to limit my comments to a subset of the facts, and to the C language only (i.e., not C++).

A #define'd (constant) value is often called a manifest constant because it is "made evident" throughout the compilation process. This is because it is a compile-time (actually pre-compile, preprocessor-time) constant. Thus, the actual value is known throughout the compilation process and some (significant) optimazations may be realized as a result. Quite complex manifest constant expressions can be constructed that when "boiled down to a low gravy", result in what you would likely do in assembly language (i.e., immediate addressing). One significant advantage for a '51 C compiler is that a "reference" to a compile-time constant has the value coded inline, so execution does not necessarily suffer from memory model dependencies and any associated inefficiencies. A downside is that a #define definition is only visible to the preprocessor. The preprocessor is only a text replacement type of thing and has no knowledge of the underlying C language, type compatibility, etc.

An advantage to having a constant data object, is that its type checking is possibly more thorough, and that its value isn't (necessarily) known through all parts of the compilation process. A const data object will be accessed at a singular (object-sized) ROM (code) or RAM (data) address. Since it has a unique address range, it can be "patched" as a configuration option if you choose.

The choice boils down to when you want to "introduce" the constant value and the associated benefits -- compile-time or run-time.

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