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]

Re: Correct behavior for const?



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>

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