Andrew Haley wrote:
Linda A. Walsh wrote:
Certainly breaks or voids the concept of equivalent expressions being
equal.
But they're not equivalent.
The key thing to remember here:
A const variable is not a constant.
const in C is just a type qualifier.
----
You are right -- However, The C Compiler still could do better.
It's not a bug, but would be an RFE in my circumstance.
The reason is that all of my 'const inst' were formed from
Pre-processor known values (none really came from actual runtime computed
data.
I rewrote the program to make it more clear about what the
improvement could have been. I wasn't misunderstanding the word const
(though I could see my meaning was unclear) -- I was using it also as
a descriptive element to remind me that everything that fed into it
came from compile-time constants.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
/* start with assumptions true */
/* Note on abbreviations used in defines:
* Ar = Array
* Asigd= Assigned
* Bltn = Builtin
* others, I hope are obvious */
#define PreProc_Groks_sizeof_ConstLenAr_div_sizeof_BltnType_min_one_is_Const 1
#define PreProc_Groks_sizeof_BltnType_AsigdTo_const_int_is_const 0
#define PreProc_Groks_sizeof_ConstLenAr_AsigdTo_const_int_is_const 0
#define PreProc_Groks_const_ints_as_const 1
static const const char * days [] ={
"Sun", "Mon", "Tue", "Wed", "Thu" , "Fri", "Sat"};
#if PreProc_Groks_sizeof_ConstLenAr_div_sizeof_BltnType_min_one_is_Const
static const int last_index = sizeof(days) / sizeof(const char *)-1;
#endif
#if PreProc_Groks_sizeof_BltnType_AsigdTo_const_int_is_const
static const int szof_charptr = sizeof(const char *);
#endif
#if PreProc_Groks_sizeof_ConstLenAr_AsigdTo_const_int_is_const
static const int szof_days = sizeof(days);
#endif
#if PreProc_Groks_sizeof_BltnType_AsigdTo_const_int_is_const
static const int sizeof_charptr = (const) szof_charptr;
#endif
#if PreProc_Groks_sizeof_ConstLenAr_AsigdTo_const_int_is_const
static const int sizeof_days = (const) szof_days;
#endif
static const int num_days = (const) sizeof(days) / sizeof(const char *);
/* fundamental issue boils down to this: PreProc does not understand consts
* assigned values that are only constants determined at Pre Preocessing time
* are not "folded" at Pre-Precessing time when they could.
*
* Not a bug -- just an optimization that could be done
* (an RFE...so nevermind...)
*
*/
#if PreProc_Groks_const_ints_as_const
static const int alt_last_index = (const) num_days - 1;
#endif
int main () {
printf("last_index = %d", last_index);
#if PreProc_Groks_const_ints_as_const
printf(", alt_last_index = %d", alt_last_index);
#endif
printf("\n");
}
/* vim: ts=4:sw=4
*/
The fact that C++ "fixes" this isn't really a difference in the language, it's just a 'next-generation' of the compile time constant folding. In "C" you'd
have to do the same thing using "#define"'s which just looked uglier...
Can you think of any backward incompatibility if "C were to rold compile
time constants the same way C++ did?
Obviously, that optimization would have to be disabled if one tried
to take the address of one of the values that has been folded as a constant.
If you don't have r/o-segment or text-write segment disabled, you could
do this in C:
static const int c=1+1;
int * trouble=&c;
++*trouble;
static const int d=c;
The pre-processor would have to do the same type of optimization disabling detection due to anonymous memory assignments that are sometimes disabled
for similar reasons when detectable at runtime. Hopefully in C++ that'd
throw an exception...
It'd have to say that anyone who got that 'fancy' at compile time should
be strung up and tickled harshly in front of their peers. :-), but it'd
probably eventually happen by accident...to anyone.
-l