memcpy这个函数使用有前提么,我在程序中用他时,提示access voilation at address xxxxxxxx,不知这是为什么。
    请教高手!!!!

解决方案 »

  1.   

    一定是memcpy时,第三个值有问题或太太,使memcpy时存取到了你不能存取的内存。
      

  2.   

    通俗的说它是在两个buffer中进行替换拷贝。例如
    char string1[] = "long long a ago" ;
    char string2[] = "aaaa bbbb c ddd" ;
    memcpy( string1+5, string2+5, 4 ) ;
    string1:就会是 long bbbb a ago ;
     出错原因可能是你要加上头文件#include <stdio.h>
      

  3.   

    我把MSDN的帮助贴出来,对照一下你自己的程序.memcpy
    Copies characters between buffers.void *memcpy( void *dest, const void *src, size_t count );Routine Required Header Compatibility 
    memcpy <memory.h> or <string.h> ANSI, Win 95, Win NT 
    For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version 
    LIBCMT.LIB Multithread static library, retail version 
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version 
    Return Valuememcpy returns the value of dest.ParametersdestNew buffersrcBuffer to copy fromcountNumber of characters to copyResThe 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.Example/* MEMCPY.C: Illustrate overlapping copy: memmove
     * handles it correctly; memcpy does not.
     */#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 ) );
    }
    OutputFunction:   memcpy without overlap
    Source:      fox
    Destination:   dog jumps over the lazy fox
    Result:      The quick brown fox jumps over the lazy fox
    Length:      43 charactersFunction:   memmove with overlap
    Source:      quick brown fox jumps over the lazy dog
    Destination:   brown fox jumps over the lazy dog
    Result:      The quick quick brown fox jumps over the lazy dog
    Length:      49 charactersFunction:   memcpy with overlap
    Source:      quick brown dog jumps over the lazy fox
    Destination:   brown dog jumps over the lazy fox
    Result:      The quick quick brown dog jumps over the lazy fox
    Length:      49 characters
      

  4.   

    谢谢众位高手  问题解决了(下附  不是原码,因为太长,不过意思是一样的)
    char *a="asdfasdfasdfasdf";
    char *b="";
    memcpy(b,a,3);
    就不对,但
    char a[100]="asdfasdfasdfasdf";
    char b[100]="";
    memcpy(b,a,3);
    就可以,难道a本身不是地址么
      

  5.   

    char *a只是一个指针,没有分配地址。
    而char a[100]已经分配了100字符长的地址了。
      

  6.   

    不对,
    若程序改为:
    char *a="asdfasdfasdfasdf"
    char b[]="";
    memcpy(b,a,3);
    没有问题.说明不是a有问题,是b有问题到底问题在哪里?我跟踪调试你原来的程序,发现到memcpy.asm中的
       mov     [edi],al        ;U - put first byte into destination
    出现access violation 
    可见b的地址必须确定!这才是错误的根源.所以,正确改动如下:
    char *a="asdfasdfasdfasdf";
    char *b=new char;
    memcpy(b,a,3);
      

  7.   

    char *p="asdfasdfasdfasdf";
    这样的话,p指向的是执行文件image中某个read-only的内存页面,你试图用memcpy写入这个地址的时候就会发生异常。char p[]="asdfasdfasdfasdf";
    这样p是一个在栈中分配的数组(如果不是全局变量的话),写入当然没有问题。