This is the mail archive of the gcc@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]
Other format: [Raw text]

RE: memcpy(p,p,len)


That would be a bug.

If h1 and h2 were marked __restrict__ then using memcpy to make the
assignment is valid, but without that marking h1 may be == h2 so memmove
is required.

	paul

> -----Original Message-----
> From: Joakim Tjernlund [mailto:joakim.tjernlund@transmode.se]
> Sent: Friday, April 30, 2010 10:15 AM
> To: Jan-Benedict Glaw
> Cc: gcc@gcc.gnu.org
> Subject: Re: memcpy(p,p,len)
> 
> Jan-Benedict Glaw <jbglaw@lug-owl.de> wrote on 2010/04/30 16:10:42:
> >
> > On Fri, 2010-04-30 16:08:15 +0200, Joakim Tjernlund
> > <joakim.tjernlund@transmode.se> wrote:
> > > Is memcpy supposed to work when the src and dest are the same:
> > > memcpy(p, p, 100);
> >
> > It may work, but you cannot rely on it. Use memmove() alternatively.
> 
> My view too, but gcc 3.4.6 on gcc does this:
> struct my_struct {
> 	long a100[100];
> };
> static inline
> func(struct my_struct *h1, struct my_struct *h2)
> {
> 	*h1 = *h2;
> }
> mytest(struct my_struct *my_h1)
> {
> 	func(my_h1, my_h1);
> }
> 
> -------------
> 	.file	"tst.c"
> 	.section	".text"
> 	.align 2
> 	.globl mytest
> 	.type	mytest, @function
> mytest:
> 	mflr 0
> 	stwu 1,-16(1)
> 	mr 4,3
> 	li 5,400
> 	stw 0,20(1)
> 	bl memcpy
> 	lwz 0,20(1)
> 	addi 1,1,16
> 	mtlr 0
> 	blr
> 	.size	mytest, .-mytest
> 	.section	.note.GNU-stack,"",@progbits
> 	.ident	"GCC: (GNU) 3.4.6 (Gentoo 3.4.6-r2, ssp-3.4.6-1.0,
> pie-8.7.9)"


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