memcpy src & dst same

Discussion in 'C Programming' started by clu, Oct 25, 2005.

  1. clu

    clu Guest

    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) );

    --
    clu
    clu, Oct 25, 2005
    #1
    1. Advertising

  2. clu wrote:
    > 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."

    --
    Peter
    Peter Nilsson, Oct 25, 2005
    #2
    1. Advertising

  3. clu

    Richard Bos Guest

    "clu" <> wrote:

    > 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
    Richard Bos, Oct 25, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Richter

    <txt src= ...> equivalent of <img src= ...>

    Steve Richter, Feb 8, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,058
    Laurent Bugnion
    Feb 9, 2006
  2. Greg Johnson
    Replies:
    4
    Views:
    3,047
  3. Spacen Jasset

    memcpy( dest, src, 0 )

    Spacen Jasset, Jun 30, 2004, in forum: C Programming
    Replies:
    8
    Views:
    404
    Spacen Jasset
    Jul 2, 2004
  4. maclab2

    [memcpy] dst=NULL,size=0

    maclab2, Mar 3, 2009, in forum: C Programming
    Replies:
    9
    Views:
    4,153
    Phil Carmody
    Mar 3, 2009
  5. Shankar

    snprintf with same src and dst

    Shankar, Dec 7, 2010, in forum: C Programming
    Replies:
    4
    Views:
    784
    Ike Naar
    Dec 8, 2010
Loading...

Share This Page