bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: restrict


From: Bruno Haible
Subject: Re: restrict
Date: Mon, 17 Feb 2020 22:03:26 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-171-generic; KDE/5.18.0; x86_64; ; )

Hi Tim,

> > Yes, and this in turn means that the ability to produce useful warnings via
> > 'restrict' is limited. In this example:
> > ===================================================================
> > #include <string.h>
> > extern void memmcpy (void *restrict, const void *restrict, size_t);
> > 
> > void shuffle (char array[10])
> > {
> >   memmcpy (array + 2, array, 8);
> >   memcpy (array + 2, array, 8);
> > }
> > ===================================================================
> > gcc gives no warning about 'memmcpy' - because it does not know
> > how many elements the function will access. gcc does give a warning
> > about 'memcpy' - apparently due to custom logic in the compiler.
> 
> The following gives you a warning, with -O2 / -O3 and -Wall:
> 
> ===================================================================
> #include <string.h>
> void memmcpy (void *restrict d, void *restrict s, size_t n)
> {
>   memcpy(d, s, n);
> }
> 
> void shuffle ()
> {
>   char array[] = "abcdefg", *array2 = array + 2;
> 
>   memmcpy (array, array2 - 2, 8);
> }
> ===================================================================

That's precisely my point: GCC can not and will not give you warnings
about overlapping array slices, unless the function is one of the
predefined functions (memcpy in this case).

'restrict' for optimization is designed for accesses to array (cf.
the many examples regarding loops that iterate over an array). But
the warnings emitted by GCC can only recognize the special case of
two pointers being equal.

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]