This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Correct behavior for const?
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Correct behavior for const?
- From: Byron Stanoszek <gandalf at winds dot org>
- Date: Sun, 8 Oct 2000 17:32:39 -0400 (EDT)
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