标签:memmove
// 若在同一块内存中拷贝,有可能实现内存重叠,为了处理这种情况,有了增强版的memcpy,与memmov功能相似 //不怕内存重叠的memcpy #include < stdio.h> #include < assert.h> void * my_memmov( void * dst, void const * src, int count ) { void * ret = dst; assert( ( dst != NULL ) && ( src != NULL ) ); //判断是否会发生内存重叠,若不发生,则执行memcpy就好 if( ( ( char * )dst < ( char * )src ) || ( ( char * )dst >= ( char * )src + count ) ) { while( count-- ) { *( ( char * )dst ) = *( ( char * )src ); ( ( char * )dst )++; ( ( char * )src )++; } return ret; } //若发生了内存重叠,那么就从后往前移 else { while( count ) { char *pdst = (char *)dst+count-1; char *psrc = (char *)src+count-1; *(pdst) = *(psrc); count--; } return ret; } return ret; } int main() { int i; int p[10] = {1,2,3,4,5,6,7,8,9}; my_memmov( p+3,p,20 ); for( i = 0; i < sizeof(p) / sizeof( p[0] ); i++ ) { printf("%d ",p[i]); } return 0; }
标签:memmove
原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/44753663