This is the mail archive of the gcc-bugs@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]

Correct behavior for const?


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


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