memcpy(c1 + 1 , c1 , 5); 这句的功能应该是把c1处的5字节拷贝到c1+1处,但是我发现,debug版本时是正确的,而release版本时就会出现错误,以下代码将memcpy的结果和memmove的结果做比较,会发现,debug版本时,memcpy和memmove的结果是相同的,而release版的结果会有不同。int _tmain(int argc, _TCHAR* argv[])
{
char c1[21]="12345678901234567890";
char c2[21]="12345678901234567890";
printf("***************************\n c1: %s\n c2: %s\n***************************\n",c1,c2);
for (int i = 1 ; i< 15 ; i ++)
{
memcpy(c1 +1 ,c1,i);
memmove(c2 +1 ,c2,i); if ( 0 != memcmp(c1,c2,20))
{
printf("Different at %d : \n",i);
printf("c1: %s \nc2: %s \n",c1,c2);
}
strcpy( c1, "12345678901234567890" );   // reset string
strcpy( c2, "12345678901234567890" );   // reset string
}
printf("\nPress any key to exit ...");
_getwch();
         return 0;
}

解决方案 »

  1.   

    编译环境 :
    Microsoft Visual C++ .NET 2003
    Windows2000 Professional SP4
      

  2.   

    release 的执行结果:
    ***************************
     c1: 12345678901234567890
     c2: 12345678901234567890
    ***************************
    Different at 2 :
    c1: 11145678901234567890
    c2: 11245678901234567890
    Different at 3 :
    c1: 11115678901234567890
    c2: 11235678901234567890
    Different at 5 :
    c1: 11234478901234567890
    c2: 11234578901234567890
    Different at 6 :
    c1: 11234448901234567890
    c2: 11234568901234567890
    Different at 7 :
    c1: 11234444901234567890
    c2: 11234567901234567890
    Different at 8 :
    c1: 11234467801234567890
    c2: 11234567801234567890
    Different at 9 :
    c1: 11234467881234567890
    c2: 11234567891234567890
    Different at 10 :
    c1: 11234467888234567890
    c2: 11234567890234567890
    Different at 11 :
    c1: 11234467888834567890
    c2: 11234567890134567890
    Different at 12 :
    c1: 11234467880124567890
    c2: 11234567890124567890
    Different at 13 :
    c1: 11234467880122567890
    c2: 11234567890123567890
    Different at 14 :
    c1: 11234467880122267890
    c2: 11234567890123467890Press any key to exit ...debug 的执行结果:
    ***************************
     c1: 12345678901234567890
     c2: 12345678901234567890
    ***************************Press any key to exit ...
      

  3.   

    你这dest和src区域重叠,使用memmove吧。另外,不要老说这个bug那么bug,ms不至于连这个最基本最基本的函数都作不对。
      

  4.   

    下面这是MSDN里面的例子,dest和src也是区域重叠的
    如果把下面的代码中的 +2 改成+1,也有这个问题!!// crt_memcpy.c
    /* Illustrate overlapping copy: memmove
     * always handles it correctly; memcpy may handle
     * it correctly.
     */#include <memory.h>
    #include <string.h>
    #include <stdio.h>char str1[7] = "aabbcc";int main( void )
    {
       printf( "The string: %s\n", str1 );
       memcpy( str1 + 2, str1, 4 );
       printf( "New string: %s\n", str1 );   strcpy( str1, "aabbcc" );   // reset string   printf( "The string: %s\n", str1 );
       memmove( str1 + 2, str1, 4 );
       printf( "New string: %s\n", str1 );
    }
    Output
    The string: aabbcc
    New string: aaaabb
    The string: aabbcc
    New string: aaaabb
      

  5.   

    这不就对了吗。这就是memcpy这个函数的行为。你还能期望什么呢?* Illustrate overlapping copy: memmove
     * always handles it correctly; memcpy may handle
     * it correctly.
     */一个always,一个may。你有重叠区域,然后用memcpy,文档说了“可能”出问题。然后真出了问题你能说是MS的bug?+1, +2出错与否都无所谓,反正是may。
      

  6.   

    先想一想:
    为什么会
    有了memcpy,还要memmove ??然后,看看MSDN对memcpy怎么说的:
    Res
    The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
      

  7.   

    MS确实有很多BUG,
    但这个恰好不是其中的一个。这个是楼主自己的BUG.
      

  8.   

    #include <memory.h>
    #include <string.h>
    #include <stdio.h>char string1[60] = "The quick brown dog jumps over the lazy fox";
    char string2[60] = "The quick brown fox jumps over the lazy dog";
    /*                           1         2         3         4         5
     *                  12345678901234567890123456789012345678901234567890
     */void main( void )
    {
       printf( "Function:\tmemcpy without overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 40 );
       printf( "Destination:\t%s\n", string1 + 16 );
       memcpy( string1 + 16, string1 + 40, 3 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );   /* Restore string1 to original contents */
       memcpy( string1 + 16, string2 + 40, 3 );   printf( "Function:\tmemmove with overlap\n" );
       printf( "Source:\t\t%s\n", string2 + 4 );
       printf( "Destination:\t%s\n", string2 + 10 );
       memmove( string2 + 10, string2 + 4, 40 );
       printf( "Result:\t\t%s\n", string2 );
       printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );   printf( "Function:\tmemcpy with overlap\n" );
       printf( "Source:\t\t%s\n", string1 + 4 );
       printf( "Destination:\t%s\n", string1 + 10 );
       memcpy( string1 + 10, string1 + 4, 40 );
       printf( "Result:\t\t%s\n", string1 );
       printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
    }
      

  9.   

    多谢2位我不是在找bug,只是在解决疑问!结贴