memcpy src & dst same

C

clu

If the source and destination pointers happen to be the same during a
memcpy(), is that going to cause undefined behavior?

int *src = &myarr[0];
int *dst = &myarr[0];

memcpy( dst, src, sizeof(myarr) );
 
P

Peter Nilsson

clu said:
If the source and destination pointers happen to be the same during a
memcpy(), is that going to cause undefined behavior?

int *src = &myarr[0];
int *dst = &myarr[0];

memcpy( dst, src, sizeof(myarr) );

Yes. 7.21.2.1p2 "... If copying takes place between objects that
overlap, the behavior is undefined."
 
R

Richard Bos

clu said:
If the source and destination pointers happen to be the same during a
memcpy(), is that going to cause undefined behavior?

int *src = &myarr[0];
int *dst = &myarr[0];

memcpy( dst, src, sizeof(myarr) );

Yes. If this is a problem, use memmove() instead.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top