This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Valid optimization with constant arrays


I noticed that for a simple testcase:
int t;
void abort (void);

int f(int t, const int *a)
{
  const int b[] = { 1, 2, 3};
  if (!t)
    return f(1, b);
  return b == a;
}

int main(void)
{
  if (f(0, 0))
    abort ();
  return 0;
}
--- CUT ---
That before 4.0 gives a different result from 4.0 and above.  Is it
valid in this case to promote b to a static variable and have f return
true when called with f(0, 0) ?

I think C99 does allows this optimization (at least according to the
normative note 112) but C++ does not.  I could not find anything in
C++ standard which says the compiler could put the const object in a
read-only section but I could be missing something somewhere.

Thanks,
Andrew Pinski


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]