我写了一个将字符串拷贝到定长字符数组的函数,如果字符串不够长则补二进制0
请问各位高手,还有什么bug,以及如何改才能获得最高执行效率?int strtoarray(const char* array, unsigned int nSize, const char* strSource)
{
assert(array);
assert(strSource); char* pArray = (char *)array;
char* pString = (char *)strSource; register unsigned int n = 0;
for(; (*strSource != 0) && (n < nSize); n++)
{
*pArray++ = *strSource++;
} if(n < nSize)
{ //长度不足, 数组后面补二进制0
for(; n < nSize; n++)
{
*pArray++ = 0;
}
}
else
{ //长度超出
return 1;
} return 0;
}

解决方案 »

  1.   

    int strtoarray(const char* array, unsigned int nSize, const char* strSource)
    {
    assert(array);
    assert(strSource);
    assert(nSize);

    int n = strlen( strSource );
    if( n >= nSize )
    {
    memcpy( array, strSource, nSize );
    return 1;
    }
    else
    {
    memcpy( array, strSource, n );
    memset( array + n, 0, nSize - n );
    }
    return 0;
    }
      

  2.   

    memcpy等库函数的实现都是按dword操作的,比搂主的方法(按字节)快4倍。但是调用这些还不是最快。“最高效率”麻烦得很,用汇编写一堆LODSD, STOSD之类最快。
      

  3.   

    hehe,我觉得还可以!~!!!!!~~~!~!~!~!