1 May 2010 02:06
Re: memcpy(p,p,len)
Joe Buck <Joe.Buck <at> synopsys.COM>
2010-05-01 00:06:58 GMT
2010-05-01 00:06:58 GMT
On Fri, Apr 30, 2010 at 08:29:19AM -0700, Richard Guenther wrote: > On Fri, Apr 30, 2010 at 5:05 PM, Joe Buck <Joe.Buck <at> synopsys.com> wrote: > > On Fri, Apr 30, 2010 at 07:30:33AM -0700, Mark Mielke wrote: > >> Just a quick comment than Jan-Benedict's opinion is widely shared by the > >> specification and by the Linux glibc manpage: > >> > >> DESCRIPTION > >> The memcpy() function copies n bytes from memory area src to > >> memory > >> area dest. The memory areas should not overlap. Use memmove(3) > >> if the > >> memory areas do overlap. > >> > >> It doesn't matter if it sometimes works. Sometimes works programs are > >> sometimes doesn't work programs.(Continue reading)> > > > The typical memcpy function will fail for overlapping but unequal memory > > ranges, but will work for src == dst. Switching to memmove would degrade > > performance, and that should only be done if there is an actual, rather > > than a theoretical bug. Note that for this use, it's not possible (if > > the program is valid) for the ranges to overlap but be unequal. > > > > Another alternative is that instead of using memcpy, a specialized > > function could be used that has the required property (the glibc > > memcpy does). > > Note that language semantics come in here as well. The middle-end > assumes that when an assignment is not BLKmode that the RHS > will be read before the lhs will be written. It does not assume so > otherwise and the behavior is undefined for overlapping *p and *q
> >
> > The typical memcpy function will fail for overlapping but unequal memory
> > ranges, but will work for src == dst. Switching to memmove would degrade
> > performance, and that should only be done if there is an actual, rather
> > than a theoretical bug. Note that for this use, it's not possible (if
> > the program is valid) for the ranges to overlap but be unequal.
> >
> > Another alternative is that instead of using memcpy, a specialized
> > function could be used that has the required property (the glibc
> > memcpy does).
>
> Note that language semantics come in here as well. The middle-end
> assumes that when an assignment is not BLKmode that the RHS
> will be read before the lhs will be written. It does not assume so
> otherwise and the behavior is undefined for overlapping *p and *q
RSS Feed