This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Correct behavior for const?
- To: Byron Stanoszek <gandalf at winds dot org>
- Subject: Re: Correct behavior for const?
- From: Geoff Keating <geoffk at cygnus dot com>
- Date: 08 Oct 2000 16:11:15 -0700
- CC: gcc-bugs at gcc dot gnu dot org
- References: <Pine.LNX.4.21.0010081723020.4599-100000@winds.org>
Byron Stanoszek <gandalf@winds.org> writes:
> 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.
Unfortunately, ISO C still permits foo to change `s' even though it is
declared `const'.
> Also, it seems adding an __attribute__ ((const)) on the foo() declaration
> has no effect. Is this correct behavior?
Arguably, no. Under those circumstances, gcc should remove the call
to foo() completely. However, this rarely makes sense and so it's not
implemented.
--
- Geoffrey Keating <geoffk@cygnus.com>