Correct behavior for const?

Byron Stanoszek gandalf@winds.org
Sun Oct 8 14:32:00 GMT 2000


I was experimenting a little with 'const' and came up with the following piece
of code. The extern of strlen() is from the GNU C library (2.1.94):


extern int strlen(const char *) __attribute__ ((pure));
extern void foo(const char *);

int main(int argc, char *argv[])
{
  char *s=argv[0], buf[256];
  int i, j;

  i=strlen(s);
  foo(s);
  j=strlen(s);

  printf("%d, %d\n", i, j);  /* use i and j */
  return 0;
}


When I look at the assembly output from gcc 2.97, it seems that 'strlen' is
called twice, even though the 'const' declaration on foo suggests that string
's' should not change. Gcc 2.95.2 also appears to inline strlen() twice.

Also, it seems adding an __attribute__ ((const)) on the foo() declaration
has no effect.  Is this correct behavior?

 -Byron

-- 
Byron Stanoszek                         Ph: (330) 644-3059
Systems Programmer                      Fax: (330) 644-8110
Commercial Timesharing Inc.             Email: bstanoszek@comtime.com



More information about the Gcc-bugs mailing list