4.
/*
* 函数介绍:拷贝整型数组;
* 输入参数:pSource,pDest,lNum;
* 输出参数:pDest;
* 返回值  :无;
*/
int *CopyIntArray(int *pDest, int *pSource, long lNum)  
{
//这程序不能定义成const int *pSource,不知如何解决pSource不能被修改的情况;
long *lpLoopNum; 
//用long lpLoopNum快还是
//用long *lpLoopNum;
int  *pLocal = pSource; try
{
lpLoopNum = new long;
//用下面循环快还是用上面的memset或别的快?
//各位能推荐一下吗?
for (*lpLoopNum=0; *lpLoopNum<lNum; (*lpLoopNum)++)    //特别是这里,希望大虾
{ //能从运行效率、可读性,方面指出
*(pDest++) = *(pLocal++);                 //最佳方案。
}
}
catch( bad_alloc error)
{
const char * pError = error.what();
cout<<"Memory Allocate Error" << pError << endl;
 //std有没有像提供AfxMessageBox信息框的调用?
} if (lpLoopNum != NULL)
delete lpLoopNum; return pTempDist;
}

解决方案 »

  1.   


    //这程序不能定义成const int *pSource,不知如何解决pSource不能被修改的情况;
    //强制转换最快的方法
    memcpy(pDest, pSource, lNum×sizeof(long))
      

  2.   

    同意上面所说.附个例子:/* 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 ) );
    }
    输出结果应为:Function:   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
      

  3.   

    另一种选择是SDK中的CopyMemory 函数.数组初始化,则用ZeroMemory